Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serializing nested ntbk metadata items #147

Merged
merged 2 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note - black did this, and it seemed reasonable + removed the line too long issues, so I went with it

)
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")