|
760 | 760 | "text/plain": [ |
761 | 761 | "[{'name': 'stdout',\n", |
762 | 762 | " 'output_type': 'stream',\n", |
763 | | - " 'text': ['CPU times: user 3 us, sys: 1e+03 ns, total: 4 us\\n',\n", |
764 | | - " 'Wall time: 5.96 us\\n']},\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", |
765 | 765 | " {'data': {'text/plain': ['2']},\n", |
766 | 766 | " 'metadata': {},\n", |
767 | 767 | " 'output_type': 'execute_result'}]" |
|
796 | 796 | " {'ename': 'ZeroDivisionError',\n", |
797 | 797 | " 'evalue': 'division by zero',\n", |
798 | 798 | " 'output_type': 'error',\n", |
799 | | - " 'traceback': 'Traceback (most recent call last):\\n File \"/home/jhoward/miniconda3/lib/python3.12/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-9e1622b385b6>\", line 1, in <module>\\n 1/0\\n ~^~\\nZeroDivisionError: division by zero\\n'}]" |
| 799 | + " 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-9e1622b385b6>\", line 1, in <module>\\n 1/0\\n ~^~\\nZeroDivisionError: division by zero\\n'}]" |
800 | 800 | ] |
801 | 801 | }, |
802 | 802 | "execution_count": null, |
|
816 | 816 | "outputs": [], |
817 | 817 | "source": [ |
818 | 818 | "#| export\n", |
819 | | - "def render_outputs(outputs, ansi_renderer=strip_ansi, include_imgs=True):\n", |
820 | | - " try: import mistletoe\n", |
| 819 | + "def _pre(s, xtra=''): return f\"<pre {xtra}><code>{escape(s)}</code></pre>\"\n", |
| 820 | + "\n", |
| 821 | + "def render_outputs(outputs, ansi_renderer=strip_ansi, include_imgs=True, pygments=False):\n", |
| 822 | + " try:\n", |
| 823 | + " from mistletoe import markdown, HTMLRenderer\n", |
| 824 | + " from mistletoe.contrib.pygments_renderer import PygmentsRenderer\n", |
821 | 825 | " except ImportError: return print('mistletoe not found -- please install it')\n", |
| 826 | + " renderer = PygmentsRenderer if pygments else HTMLRenderer\n", |
822 | 827 | " def render_output(out):\n", |
823 | 828 | " otype = out['output_type']\n", |
824 | 829 | " if otype == 'stream':\n", |
825 | 830 | " txt = ansi_renderer(''.join(out['text']))\n", |
826 | | - " return f\"<pre>{txt}</pre>\" if out['name']=='stdout' else f\"<pre class='stderr'>{txt}</pre>\"\n", |
| 831 | + " return _pre(txt, '' if out['name']=='stdout' else \"class='stderr'\")\n", |
827 | 832 | " elif otype in ('display_data','execute_result'):\n", |
828 | 833 | " data = out['data']\n", |
829 | 834 | " _g = lambda t: ''.join(data[t]) if t in data else None\n", |
830 | 835 | " if d := _g('text/html'): return d\n", |
831 | 836 | " if d := _g('application/javascript'): return f'<script>{d}</script>'\n", |
832 | | - " if d := _g('text/markdown'): return mistletoe.markdown(d)\n", |
| 837 | + " if d := _g('text/markdown'): return markdown(d, renderer=renderer)\n", |
833 | 838 | " if d := _g('text/latex'): return f'<div class=\"math\">${d}$</div>'\n", |
834 | | - " if d := _g('text/plain'): return f\"<pre>{escape(d)}</pre>\"\n", |
| 839 | + " if d := _g('text/plain'): return _pre(d)\n", |
835 | 840 | " if d := _g('image/svg+xml'): return d\n", |
836 | 841 | " if include_imgs:\n", |
837 | 842 | " if d := _g('image/jpeg'): return f'<img src=\"data:image/jpeg;base64,{d}\"/>'\n", |
|
0 commit comments