fix(jupyter): fixing rendering of legacy jupyter notebooks. Fixes|References: RCCE-10

This commit is contained in:
Serhii Ilin 2023-12-28 13:17:30 +02:00
parent 465599f03f
commit 16872b03e1
2 changed files with 104 additions and 2 deletions

View file

@ -475,7 +475,8 @@ class MarkupRenderer(object):
def as_html(notebook):
conf = config.Config()
conf.CustomHTMLExporter.preprocessors = [Sandbox]
conf.CustomHTMLExporter.default_preprocessors = [Sandbox]
conf.Sandbox.enabled = True
html_exporter = CustomHTMLExporter(config=conf)
(body, resources) = html_exporter.from_notebook_node(notebook)
@ -518,7 +519,8 @@ class MarkupRenderer(object):
body = '\n'.join([header, css, js, body])
return body, resources
notebook = nbformat.reads(source, as_version=nbformat.NO_CONVERT)
# TODO: In the event of a newer jupyter notebook version, consider increasing the as_version parameter
notebook = nbformat.reads(source, as_version=4)
(body, resources) = as_html(notebook)
return body

View file

@ -683,3 +683,103 @@ def test_relative_path(src_path, server_path, is_path, expected):
def test_relative_links(src_html, expected_html):
server_paths = {'raw': '/path/raw/file.md', 'standard': '/path/file.md'}
assert relative_links(src_html, server_paths=server_paths) == expected_html
@pytest.mark.parametrize("notebook_source, expected_output", [
("""
{
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\\n"
]
}
],
"input": "print('Hello, World!')"
}
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
}
}
""", "Hello, World!"),
("""
{
"nbformat": 4,
"nbformat_minor": 1,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\\n"
]
}
],
"source": [
"print('Hello, World!')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 4
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
}
}
""", "Hello, World!")
])
def test_jp_notebook_html_generation(notebook_source, expected_output):
import mock
with mock.patch('rhodecode.lib.helpers.asset'):
body = MarkupRenderer.jupyter(notebook_source)
assert "<!-- ## IPYTHON NOTEBOOK RENDERING ## -->" in body
assert expected_output in body