-
Notifications
You must be signed in to change notification settings - Fork 208
ENH Add functionality to modify Jupyterlite notebooks based on their content #1113
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
ENH Add functionality to modify Jupyterlite notebooks based on their content #1113
Conversation
I was going to complain that you need to u pdate the doc but I see you're not at that stage yet :) @StefRe can you look to see if this makes sense? |
Yep planning to write doc + tests for this when the implementation has settled down a bit. The main things I am wondering about right now (mentioned in the TODOs), what should the signature be for the function that modify a Jupyterlite notebook (and is user-defined in
|
@larsoner: The approach seems reasonable to me, looks good. @lesteve: I'd prefer |
+1 for dict and filename |
I think this is ready for review.
|
@StefRe can you take a look? If you're happy with the API and docs I'll do a pass and merge |
doc/configuration.rst
Outdated
when you do ``json.load(open(notebook_filename))``. The function is expected | ||
to modify ``json_dict`` in place by adding notebook cells. It is not expected | ||
to write to the file, since ``sphinx-gallery`` is in charge of this. | ||
``notebook_filename`` is provided for convenience becauses it is useful to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: becauses --> because
notebook_modification_function(notebook_content, notebook_filename) | ||
|
||
with open(notebook_filename, "w") as f: | ||
json.dump(notebook_content, f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add here indent=2
as in
sphinx-gallery/sphinx_gallery/notebook.py
Line 347 in 7f7087d
json.dump(work_notebook, out_nb, indent=2) |
so that the modified notebook keeps its readability
doc/configuration.rst
Outdated
@@ -1196,9 +1213,11 @@ extra things will happen: | |||
2. The built Jupyter Notebooks from the documentation will be copied to a | |||
folder called ``<jupyterlite_contents>/`` (relative to Sphinx source | |||
directory) | |||
3. The rST output of each Sphinx-Gallery example will now have a | |||
3. If ``notebook_modification_function`` is not None, this function is going to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None --> None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lesteve maybe you could also fix the double "some" in the existing docs https://github.com/sphinx-gallery/sphinx-gallery/blame/master/doc/configuration.rst#L1134:
instead of
and also remove 2 of the 3 |
Thanks a lot for the review, I think I have tackled all of the comments, let me know if you have some more! |
@lesteve Perfect, no more comments 👍 . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one tiny idea, otherwise LGTM!
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
Thanks @lesteve ! |
… to version 0.13.0 v0.13.0 ------- **Implemented enhancements:** - ENH: Create backreferences for default roles `#1122 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1122>`__ (`StefRe <https://github.com/StefRe>`__) - ENH raise error in check_jupyterlite_conf with unknown key `#1119 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1119>`__ (`lesteve <https://github.com/lesteve>`__) - ENH Add functionality to modify Jupyterlite notebooks based on their content `#1113 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1113>`__ (`lesteve <https://github.com/lesteve>`__) - ENH: Add support for WebP `#1111 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1111>`__ (`StefRe <https://github.com/StefRe>`__) **Fixed bugs:** - ENH Clean-up code by early initialization of sphinx_gallery_conf `#1120 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1120>`__ (`lesteve <https://github.com/lesteve>`__) - FIX JupyterLite button links `#1115 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1115>`__ (`lesteve <https://github.com/lesteve>`__) - Fix thumbnail text formatting `#1108 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1108>`__ (`StefRe <https://github.com/StefRe>`__) (NEWS truncated at 15 lines)
To be able to run in JupyterLite, notebooks may need additional code, e.g.
%pip install seaborn
sinceseaborn
(or any pure-Python code) is not part of Pyodide.Also this allows to put a warning in a markdown cell to manage user expectations at the beginning of the notebook. We could add a better warning for notebooks that we don't expect to run any time soon, e.g. the ones using pyvista.
No tests or doc for now, but this still may be enough to get some feed-back on the code.
xref #1100