From 4d57fd872bff39f8402b161e7b0b3e752eb464b4 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Wed, 8 Apr 2020 16:54:32 -0700 Subject: [PATCH] serializing nested ntbk metadata items (#147) * serializing nested ntbk metadata items * updating tests for nested metadata --- myst_nb/parser.py | 20 ++++---------------- tests/test_parser.py | 34 ++++++++++++++++++++++++++++++---- tests/test_text_based.py | 13 ++++++++++++- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/myst_nb/parser.py b/myst_nb/parser.py index 403dd0ae..99670f0f 100644 --- a/myst_nb/parser.py +++ b/myst_nb/parser.py @@ -117,7 +117,7 @@ def parse_block(src, start_line): for cell_index, nb_cell in enumerate(ntbk.cells): - # if the the source_map ahs been stored (for text-based notebooks), + # if the the source_map has been stored (for text-based notebooks), # we use that do define the starting line for each cell # otherwise, we set a pseudo base that represents the cell index start_line = source_map[cell_index] if source_map else (cell_index + 1) * 10000 @@ -162,22 +162,10 @@ def parse_block(src, start_line): md.core.process(state) # Add the front matter. - # Note that myst_parser now serialises dict/list like keys, when rendering to - # docutils docinfo, - # so to stay consistent with the previous code (for now) we strip this data + # Note that myst_parser serialises dict/list like keys, when rendering to + # docutils docinfo. These could be read back with `json.loads`. state.tokens = [ - Token( - "front_matter", - "", - 0, - content=( - { - k: v - for k, v in ntbk.metadata.items() - if isinstance(v, (str, int, float)) - } - ), - ) + Token("front_matter", "", 0, content=({k: v for k, v in ntbk.metadata.items()})) ] + state.tokens # If there are widgets, this will embed the state of all widgets in a script diff --git a/tests/test_parser.py b/tests/test_parser.py index 7f1860ac..199b97a2 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -6,7 +6,16 @@ def test_basic_run(sphinx_run, file_regression): sphinx_run.build() # print(sphinx_run.status()) assert sphinx_run.warnings() == "" - assert sphinx_run.app.env.metadata == {"basic_run": {"test_name": "notebook1"}} + assert set(sphinx_run.app.env.metadata["basic_run"].keys()) == { + "test_name", + "kernelspec", + "language_info", + } + assert sphinx_run.app.env.metadata["basic_run"]["test_name"] == "notebook1" + assert ( + sphinx_run.app.env.metadata["basic_run"]["kernelspec"] + == '{"display_name": "Python 3", "language": "python", "name": "python3"}' + ) file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml") filenames = { @@ -20,11 +29,28 @@ def test_basic_run(sphinx_run, file_regression): ) def test_complex_outputs(sphinx_run, file_regression): sphinx_run.build() - # print(sphinx_run.status()) assert sphinx_run.warnings() == "" - assert sphinx_run.app.env.metadata == { - "complex_outputs": {"celltoolbar": "Edit Metadata", "hide_input": "False"} + + assert set(sphinx_run.app.env.metadata["complex_outputs"].keys()) == { + "ipub", + "hide_input", + "nav_menu", + "celltoolbar", + "latex_envs", + "kernelspec", + "language_info", + "jupytext", + "toc", + "varInspector", } + assert ( + sphinx_run.app.env.metadata["complex_outputs"]["celltoolbar"] == "Edit Metadata" + ) + assert sphinx_run.app.env.metadata["complex_outputs"]["hide_input"] == "False" + assert ( + sphinx_run.app.env.metadata["complex_outputs"]["kernelspec"] + == '{"display_name": "Python 3", "language": "python", "name": "python3"}' + ) file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml") filenames = { diff --git a/tests/test_text_based.py b/tests/test_text_based.py index ceb059be..f0989621 100644 --- a/tests/test_text_based.py +++ b/tests/test_text_based.py @@ -9,6 +9,17 @@ def test_basic_run(sphinx_run, file_regression, check_nbs): sphinx_run.build() # print(sphinx_run.status()) assert sphinx_run.warnings() == "" - assert sphinx_run.app.env.metadata == {"basic_unrun": {"author": "Chris"}} + assert set(sphinx_run.app.env.metadata["basic_unrun"].keys()) == { + "jupytext", + "kernelspec", + "author", + "source_map", + "language_info", + } + assert sphinx_run.app.env.metadata["basic_unrun"]["author"] == "Chris" + assert ( + sphinx_run.app.env.metadata["basic_unrun"]["kernelspec"] + == '{"display_name": "Python 3", "language": "python", "name": "python3"}' + ) file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb") file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")