Skip to content

Commit 5ead10a

Browse files
committed
ansi renderers escape automatically
1 parent ff083a3 commit 5ead10a

File tree

3 files changed

+71
-55
lines changed

3 files changed

+71
-55
lines changed

execnb/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'execnb.shell._out_nb': ('shell.html#_out_nb', 'execnb/shell.py'),
5050
'execnb.shell._out_stream': ('shell.html#_out_stream', 'execnb/shell.py'),
5151
'execnb.shell._pre': ('shell.html#_pre', 'execnb/shell.py'),
52+
'execnb.shell._strip': ('shell.html#_strip', 'execnb/shell.py'),
5253
'execnb.shell.exec_nb': ('shell.html#exec_nb', 'execnb/shell.py'),
5354
'execnb.shell.find_output': ('shell.html#find_output', 'execnb/shell.py'),
5455
'execnb.shell.format_exc': ('shell.html#format_exc', 'execnb/shell.py'),

execnb/shell.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ def run(self:CaptureShell,
138138

139139
# %% ../nbs/02_shell.ipynb
140140
def _pre(s, xtra=''): return f"<pre {xtra}><code>{escape(s)}</code></pre>"
141+
def _strip(s): return strip_ansi(escape(s))
141142

142-
def render_outputs(outputs, ansi_renderer=strip_ansi, include_imgs=True, pygments=False):
143+
def render_outputs(outputs, ansi_renderer=_strip, include_imgs=True, pygments=False):
143144
try:
144145
from mistletoe import markdown, HTMLRenderer
145146
from mistletoe.contrib.pygments_renderer import PygmentsRenderer
@@ -149,7 +150,8 @@ def render_output(out):
149150
otype = out['output_type']
150151
if otype == 'stream':
151152
txt = ansi_renderer(''.join(out['text']))
152-
return _pre(txt, '' if out['name']=='stdout' else "class='stderr'")
153+
xtra = '' if out['name']=='stdout' else "class='stderr'"
154+
return f"<pre {xtra}><code>{txt}</code></pre>"
153155
elif otype in ('display_data','execute_result'):
154156
data = out['data']
155157
_g = lambda t: ''.join(data[t]) if t in data else None

nbs/02_shell.ipynb

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@
760760
"text/plain": [
761761
"[{'name': 'stdout',\n",
762762
" 'output_type': 'stream',\n",
763-
" 'text': ['CPU times: user 1 us, sys: 1e+03 ns, total: 2 us\\n',\n",
764-
" 'Wall time: 5.01 us\\n']},\n",
763+
" 'text': ['CPU times: user 1 us, sys: 0 ns, total: 1 us\\n',\n",
764+
" 'Wall time: 3.1 us\\n']},\n",
765765
" {'data': {'text/plain': ['2']},\n",
766766
" 'metadata': {},\n",
767767
" 'output_type': 'execute_result'}]"
@@ -777,6 +777,60 @@
777777
"o"
778778
]
779779
},
780+
{
781+
"cell_type": "markdown",
782+
"metadata": {},
783+
"source": [
784+
"The result of the last successful execution is stored in `result`:"
785+
]
786+
},
787+
{
788+
"cell_type": "code",
789+
"execution_count": null,
790+
"metadata": {},
791+
"outputs": [
792+
{
793+
"data": {
794+
"text/plain": [
795+
"2"
796+
]
797+
},
798+
"execution_count": null,
799+
"metadata": {},
800+
"output_type": "execute_result"
801+
}
802+
],
803+
"source": [
804+
"s.result"
805+
]
806+
},
807+
{
808+
"cell_type": "markdown",
809+
"metadata": {},
810+
"source": [
811+
"A trailing `;` stops the result from being captured:"
812+
]
813+
},
814+
{
815+
"cell_type": "code",
816+
"execution_count": null,
817+
"metadata": {},
818+
"outputs": [
819+
{
820+
"data": {
821+
"text/plain": [
822+
"[]"
823+
]
824+
},
825+
"execution_count": null,
826+
"metadata": {},
827+
"output_type": "execute_result"
828+
}
829+
],
830+
"source": [
831+
"s.run(\"1+2;\")"
832+
]
833+
},
780834
{
781835
"cell_type": "code",
782836
"execution_count": null,
@@ -817,8 +871,9 @@
817871
"source": [
818872
"#| export\n",
819873
"def _pre(s, xtra=''): return f\"<pre {xtra}><code>{escape(s)}</code></pre>\"\n",
874+
"def _strip(s): return strip_ansi(escape(s))\n",
820875
"\n",
821-
"def render_outputs(outputs, ansi_renderer=strip_ansi, include_imgs=True, pygments=False):\n",
876+
"def render_outputs(outputs, ansi_renderer=_strip, include_imgs=True, pygments=False):\n",
822877
" try:\n",
823878
" from mistletoe import markdown, HTMLRenderer\n",
824879
" from mistletoe.contrib.pygments_renderer import PygmentsRenderer\n",
@@ -828,7 +883,8 @@
828883
" otype = out['output_type']\n",
829884
" if otype == 'stream':\n",
830885
" txt = ansi_renderer(''.join(out['text']))\n",
831-
" return _pre(txt, '' if out['name']=='stdout' else \"class='stderr'\")\n",
886+
" xtra = '' if out['name']=='stdout' else \"class='stderr'\"\n",
887+
" return f\"<pre {xtra}><code>{txt}</code></pre>\"\n",
832888
" elif otype in ('display_data','execute_result'):\n",
833889
" data = out['data']\n",
834890
" _g = lambda t: ''.join(data[t]) if t in data else None\n",
@@ -854,13 +910,13 @@
854910
{
855911
"data": {
856912
"text/html": [
857-
"<pre>---------------------------------------------------------------------------\n",
913+
"<pre ><code>---------------------------------------------------------------------------\n",
858914
"ZeroDivisionError Traceback (most recent call last)\n",
859-
"File <ipython-input-1-9e1622b385b6>:1\n",
860-
"----> 1 1/0\n",
915+
"File &lt;ipython-input-1-9e1622b385b6&gt;:1\n",
916+
"----&gt; 1 1/0\n",
861917
"\n",
862918
"ZeroDivisionError: division by zero\n",
863-
"</pre>\n"
919+
"</code></pre>\n"
864920
],
865921
"text/plain": [
866922
"<IPython.core.display.HTML object>"
@@ -890,13 +946,13 @@
890946
{
891947
"data": {
892948
"text/html": [
893-
"<pre><span class=\"ansi-red-fg\">---------------------------------------------------------------------------</span>\n",
949+
"<pre ><code><span class=\"ansi-red-fg\">---------------------------------------------------------------------------</span>\n",
894950
"<span class=\"ansi-red-fg\">ZeroDivisionError</span> Traceback (most recent call last)\n",
895951
"File <span class=\"ansi-green-fg\">&lt;ipython-input-1-9e1622b385b6&gt;:1</span>\n",
896952
"<span class=\"ansi-green-fg\">----&gt; 1</span> <span style=\"color: rgb(98,98,98)\">1</span><span style=\"color: rgb(98,98,98)\">/</span><span style=\"color: rgb(98,98,98)\">0</span>\n",
897953
"\n",
898954
"<span class=\"ansi-red-fg\">ZeroDivisionError</span>: division by zero\n",
899-
"</pre>\n"
955+
"</code></pre>\n"
900956
],
901957
"text/plain": [
902958
"<IPython.core.display.HTML object>"
@@ -911,49 +967,6 @@
911967
"HTML(render_outputs(o, ansi2html))"
912968
]
913969
},
914-
{
915-
"cell_type": "markdown",
916-
"metadata": {},
917-
"source": [
918-
"The result of the last successful execution is stored in `result`:"
919-
]
920-
},
921-
{
922-
"cell_type": "code",
923-
"execution_count": null,
924-
"metadata": {},
925-
"outputs": [],
926-
"source": [
927-
"s.result"
928-
]
929-
},
930-
{
931-
"cell_type": "markdown",
932-
"metadata": {},
933-
"source": [
934-
"A trailing `;` stops the result from being captured:"
935-
]
936-
},
937-
{
938-
"cell_type": "code",
939-
"execution_count": null,
940-
"metadata": {},
941-
"outputs": [
942-
{
943-
"data": {
944-
"text/plain": [
945-
"[]"
946-
]
947-
},
948-
"execution_count": null,
949-
"metadata": {},
950-
"output_type": "execute_result"
951-
}
952-
],
953-
"source": [
954-
"s.run(\"1+2;\")"
955-
]
956-
},
957970
{
958971
"cell_type": "markdown",
959972
"metadata": {},

0 commit comments

Comments
 (0)