Skip to content
Closed
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
10 changes: 6 additions & 4 deletions fastcore/nbio.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,20 +542,22 @@ def _render_md(out, html1st=True):
mime,d = preferred_msg_out(out, html1st=html1st, include_imgs=True)
d = _join(d)
if not d: return None
if mime=='text/plain': return 'txt', strip_ansi(d)
if mime=='text/plain': return 'txt', d
elif mime=='text/html': return 'md', fenced(d.strip(), '{=html}')
elif mime=='application/javascript': return 'md', fenced(f'<script>{d}</script>', '{=html}')
elif mime=='image/svg+xml': return 'md', fenced(d.strip(), '{=html}')
elif mime in ('text/markdown','text/latex'): return 'md', d.strip()
elif mime in ('image/jpeg','image/png'): return 'md', f'![](data:{mime};base64,{"".join(d.split())})'
return None

def render_md(outputs, html1st=True):
"Render notebook outputs as Markdown: text pooled into ```output fences, HTML in `{=html}` fences, markdown inlined, images as data URIs"
def render_md(outputs, html1st=True, ansi='strip'):
"Render notebook outputs as Markdown: text pooled into ```output fences, HTML in `{=html}` fences, markdown inlined, images as data URIs; `ansi='html'` renders ANSI colors in pooled text as an `{=html}` block instead of stripping them"
if (not isinstance(outputs, (list,tuple))) or (outputs and not isinstance(outputs[0],dict)): return ''
parts,buf = [],[]
def _flush():
if (txt := ''.join(buf).rstrip()): parts.append(fenced(txt, 'output'))
if (txt := ''.join(buf).rstrip()):
if ansi=='html' and '\x1b' in txt: parts.append(fenced(f'<pre><code class="ansi">{ansi2html(txt)}</code></pre>', '{=html}'))
else: parts.append(fenced(strip_ansi(txt), 'output'))
buf.clear()
for out in concat_streams(outputs):
if not (r := _render_md(out, html1st=html1st)): continue
Expand Down
16 changes: 11 additions & 5 deletions nbs/13_nbio.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2028,20 +2028,22 @@
" mime,d = preferred_msg_out(out, html1st=html1st, include_imgs=True)\n",
" d = _join(d)\n",
" if not d: return None\n",
" if mime=='text/plain': return 'txt', strip_ansi(d)\n",
" if mime=='text/plain': return 'txt', d\n",
" elif mime=='text/html': return 'md', fenced(d.strip(), '{=html}')\n",
" elif mime=='application/javascript': return 'md', fenced(f'<script>{d}</script>', '{=html}')\n",
" elif mime=='image/svg+xml': return 'md', fenced(d.strip(), '{=html}')\n",
" elif mime in ('text/markdown','text/latex'): return 'md', d.strip()\n",
" elif mime in ('image/jpeg','image/png'): return 'md', f'![](data:{mime};base64,{\"\".join(d.split())})'\n",
" return None\n",
"\n",
"def render_md(outputs, html1st=True):\n",
" \"Render notebook outputs as Markdown: text pooled into ```output fences, HTML in `{=html}` fences, markdown inlined, images as data URIs\"\n",
"def render_md(outputs, html1st=True, ansi='strip'):\n",
" \"Render notebook outputs as Markdown: text pooled into ```output fences, HTML in `{=html}` fences, markdown inlined, images as data URIs; `ansi='html'` renders ANSI colors in pooled text as an `{=html}` block instead of stripping them\"\n",
" if (not isinstance(outputs, (list,tuple))) or (outputs and not isinstance(outputs[0],dict)): return ''\n",
" parts,buf = [],[]\n",
" def _flush():\n",
" if (txt := ''.join(buf).rstrip()): parts.append(fenced(txt, 'output'))\n",
" if (txt := ''.join(buf).rstrip()):\n",
" if ansi=='html' and '\\x1b' in txt: parts.append(fenced(f'<pre><code class=\"ansi\">{ansi2html(txt)}</code></pre>', '{=html}'))\n",
" else: parts.append(fenced(strip_ansi(txt), 'output'))\n",
" buf.clear()\n",
" for out in concat_streams(outputs):\n",
" if not (r := _render_md(out, html1st=html1st)): continue\n",
Expand Down Expand Up @@ -2088,7 +2090,11 @@
"\n",
"test_eq(render_md([mk_display(image_png='abc')]), '![](data:image/png;base64,abc)')\n",
"test_eq(render_md([mk_error('oops')]), fenced('oops', 'output'))\n",
"test_eq(render_md([mk_stream('stdout', 'a `` b\\n')]), '```output\\na `` b\\n```')"
"test_eq(render_md([mk_stream('stdout', 'a `` b\\n')]), '```output\\na `` b\\n```')\n",
"s = '\\x1b[1;31mred\\x1b[0m <b>\\n'\n",
"test_eq(render_md([mk_stream('stdout', s)]), fenced('red <b>', 'output'))\n",
"h = render_md([mk_stream('stdout', s)], ansi='html')\n",
"assert h.startswith('```{=html}') and 'ansi-red-intense-fg' in h and '&lt;b&gt;' in h"
]
},
{
Expand Down
Loading