Benjamin Greiner
6ce6acd292
- Modify Example.ipynb because nbval 0.10 abolished saving the input history OBS-URL: https://build.opensuse.org/request/show/1062690 OBS-URL: https://build.opensuse.org/package/show/devel:languages:python:jupyter/python-jupyterlab-pygments?expand=0&rev=14
73 lines
1.6 KiB
Plaintext
73 lines
1.6 KiB
Plaintext
{
|
|
"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'<div class=\"highlight\">{html}</div>'\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
|
|
}
|