Skip to content

Commit

Permalink
serializing nested ntbk metadata items (executablebooks#147)
Browse files Browse the repository at this point in the history
* serializing nested ntbk metadata items
* updating tests for nested metadata
  • Loading branch information
choldgraf authored and phaustin committed Apr 10, 2020
1 parent 4126539 commit 90377b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
20 changes: 4 additions & 16 deletions myst_nb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 30 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
13 changes: 12 additions & 1 deletion tests/test_text_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 90377b6

Please sign in to comment.