Skip to content

Commit c6c627b

Browse files
author
ncoop57
committed
Add optional image rendering control to render_outputs
1 parent feeab61 commit c6c627b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

execnb/shell.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def run(self:CaptureShell,
137137
return _out_nb(res, self.display_formatter)
138138

139139
# %% ../nbs/02_shell.ipynb
140-
def render_outputs(outputs, ansi_renderer=strip_ansi):
140+
def render_outputs(outputs, ansi_renderer=strip_ansi, include_imgs=True):
141141
try: import mistletoe
142142
except ImportError: return print('mistletoe not found -- please install it')
143143
def render_output(out):
@@ -151,11 +151,12 @@ def render_output(out):
151151
if d := _g('text/html'): return d
152152
if d := _g('application/javascript'): return f'<script>{d}</script>'
153153
if d := _g('text/markdown'): return mistletoe.markdown(d)
154-
if d := _g('image/svg+xml'): return d
155-
if d := _g('image/jpeg'): return f'<img src="data:image/jpeg;base64,{d}"/>'
156-
if d := _g('image/png'): return f'<img src="data:image/png;base64,{d}"/>'
157154
if d := _g('text/latex'): return f'<div class="math">${d}$</div>'
158155
if d := _g('text/plain'): return f"<pre>{escape(d)}</pre>"
156+
if d := _g('image/svg+xml'): return d
157+
if include_imgs:
158+
if d := _g('image/jpeg'): return f'<img src="data:image/jpeg;base64,{d}"/>'
159+
if d := _g('image/png'): return f'<img src="data:image/png;base64,{d}"/>'
159160
return ''
160161

161162
return '\n'.join(map(render_output, outputs))

nbs/02_shell.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@
816816
"outputs": [],
817817
"source": [
818818
"#| export\n",
819-
"def render_outputs(outputs, ansi_renderer=strip_ansi):\n",
819+
"def render_outputs(outputs, ansi_renderer=strip_ansi, include_imgs=True):\n",
820820
" try: import mistletoe\n",
821821
" except ImportError: return print('mistletoe not found -- please install it')\n",
822822
" def render_output(out):\n",
@@ -830,11 +830,12 @@
830830
" if d := _g('text/html'): return d\n",
831831
" if d := _g('application/javascript'): return f'<script>{d}</script>'\n",
832832
" if d := _g('text/markdown'): return mistletoe.markdown(d)\n",
833-
" if d := _g('image/svg+xml'): return d\n",
834-
" if d := _g('image/jpeg'): return f'<img src=\"data:image/jpeg;base64,{d}\"/>'\n",
835-
" if d := _g('image/png'): return f'<img src=\"data:image/png;base64,{d}\"/>'\n",
836833
" if d := _g('text/latex'): return f'<div class=\"math\">${d}$</div>'\n",
837834
" if d := _g('text/plain'): return f\"<pre>{escape(d)}</pre>\"\n",
835+
" if d := _g('image/svg+xml'): return d\n",
836+
" if include_imgs:\n",
837+
" if d := _g('image/jpeg'): return f'<img src=\"data:image/jpeg;base64,{d}\"/>'\n",
838+
" if d := _g('image/png'): return f'<img src=\"data:image/png;base64,{d}\"/>'\n",
838839
" return ''\n",
839840
" \n",
840841
" return '\\n'.join(map(render_output, outputs))"

0 commit comments

Comments
 (0)