Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions src/rich_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,11 +894,16 @@ def render_ipynb(
elif output_type == "error":
renderable = Text.from_ansi("\n".join(output["traceback"]).rstrip())
new_line = True
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"
)
elif output_type in ("execute_result", "display_data"):
if output_type == "execute_result":
execution_count = output.get("execution_count", " ") or " "
renderable = Text.from_markup(
f"[red]Out[[#ee4b2b]{execution_count}[/#ee4b2b]]:[/red]\n"
)
elif "text/plain" not in output["data"]:
continue
else:
renderable = Text()
data = output["data"].get("text/plain", "")
if isinstance(data, list):
renderable += Text.from_ansi("".join(data))
Expand Down
49 changes: 49 additions & 0 deletions tests/test_render_ipynb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json

import pytest
from click.testing import CliRunner


@pytest.mark.parametrize("output_type", ["display_data", "execute_result"])
@pytest.mark.parametrize(
"plain_text", ["DISPLAY_FIRST\nDISPLAY_LAST", ["DISPLAY_FIRST\n", "DISPLAY_LAST"]]
)
def test_notebook_plain_text_outputs_are_rendered(tmp_path, output_type, plain_text):
from rich_cli.__main__ import main

output = {
"output_type": output_type,
"data": {"text/plain": plain_text},
"metadata": {},
}
if output_type == "execute_result":
output["execution_count"] = 7
notebook = {
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python",
"language": "python",
}
},
"cells": [
{
"cell_type": "code",
"id": "display-output",
"metadata": {},
"execution_count": 7,
"source": ["pass\n"],
"outputs": [output],
}
],
}
path = tmp_path / "display.ipynb"
path.write_text(json.dumps(notebook), encoding="utf-8")
result = CliRunner().invoke(main, [str(path)])
assert result.exit_code == 0, repr(result.exception)
assert "DISPLAY_FIRST" in result.output
assert "DISPLAY_LAST" in result.output
assert result.output.index("DISPLAY_FIRST") < result.output.index("DISPLAY_LAST")
assert ("Out[" in result.output) == (output_type == "execute_result")