{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pygments import highlight\n", "from pygments.lexers import PythonLexer\n", "from pygments.formatters import HtmlFormatter\n", "from jupyterlab_pygments import JupyterStyle" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mycode = \"\"\"\n", "class PythonCode:\n", " def __init__(self, value):\n", " self.value = value\n", " \n", " def _repr_html_(self, **kwargs):\n", " formatter = HtmlFormatter(style=JupyterStyle)\n", " html = highlight(self.value, PythonLexer(), formatter)\n", " return f'
{html}
'\n", "\"\"\"\n", "exec(mycode)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rendering the previous cell in HTML with Pygments and JupyterStyle respects the JupyterLab theme" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "PythonCode(mycode)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 4 }