Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions nbread/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def wrapped_print(text):
import json
from rich.syntax import Syntax
from rich.console import Group
from rich.panel import Panel

notebook_str = read_resource(resource)
notebook_dict = json.loads(notebook_str)
Expand All @@ -153,18 +152,20 @@ def wrapped_print(text):
if new_line:
wrapped_print("")

if "execution_count" in cell:
execution_count = cell["execution_count"] or " "
wrapped_print(
f"[green]In [[#66ff00]{execution_count}[/#66ff00]]:[/green]"
)

source = "".join(cell["source"])

if cell["cell_type"] == "code":
num_lines = len(source.splitlines())
line_range = _line_range(head, tail, num_lines)
renderable = Panel(

# Create labeled separator
execution_count = cell.get("execution_count", " ") or " "
label = f"── Code [{execution_count}] "
separator_width = console.width - 8 - len(label)
separator = Text(label + "─" * separator_width, style="dim")

renderable = Group(
separator,
Syntax(
source,
lexer,
Expand All @@ -174,7 +175,6 @@ def wrapped_print(text):
word_wrap=True,
line_range=line_range,
),
border_style="dim",
)
elif cell["cell_type"] == "markdown":
renderable = Markdown(source, code_theme=theme, hyperlinks=hyperlinks)
Expand All @@ -190,7 +190,12 @@ def wrapped_print(text):
output_type = output["output_type"]

if output_type == "stream":
renderable = Text.from_ansi("".join(output["text"]))
execution_count = cell.get("execution_count", " ") or " "
renderable = Text.from_markup(
f"\n[red]Out[[#ee4b2b]{execution_count}[/#ee4b2b]]:[/red]\n"
)
renderable += Text.from_ansi("".join(output["text"]))
renderable += Text("\n")
new_line = False

elif output_type == "error":
Expand All @@ -200,13 +205,14 @@ def wrapped_print(text):
elif output_type == "execute_result":
execution_count = output.get("execution_count", " ") or " "
renderable = Text.from_markup(
f"[red]Out[[#ee4b2b]{execution_count}[/#ee4b2b]]:[/red]\n"
f"\n[red]Out[[#ee4b2b]{execution_count}[/#ee4b2b]]:[/red]\n"
)
data = output["data"].get("text/plain", "")
if isinstance(data, list):
renderable += Text.from_ansi("".join(data))
else:
renderable += Text.from_ansi(data)
renderable += Text("\n")
new_line = True

elif output_type == "display_data":
Expand Down