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
Notebook to sphinx-gallery script #4854
Notebook to sphinx-gallery script #4854
Conversation
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
Codecov Report
@@ Coverage Diff @@
## main #4854 +/- ##
==========================================
- Coverage 75.63% 75.62% -0.02%
==========================================
Files 226 226
Lines 33029 33069 +40
==========================================
+ Hits 24983 25007 +24
- Misses 8046 8062 +16
... and 13 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
Thanks @MRegeard...
A naive question: have you try to take a notebook build by our doc (e.g. docs/tutorials/api/makers.ipynb) and compare the output of this code to the initial python code?
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
@bkhelifi, Yes I checked that ! The only thing that is different is that it put The other thing that it was doing was adding a
at the beginning and at the end when we made reference to the code using Tell me if anyone has a strong opinion on the `====, I will find a way ! |
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.
Thank you for this much needed addition.
Maybe you can build the docs locally on your machine once to ensure that the rendered notebooks have no issues with the ====
@AtreyeeS, That's what I have done ! So it does not change anything, at least that I could notice. But I just notice that we are using indistinctly |
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.
Thanks @MRegeard.
Just made some minor 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.
Thanks @MRegeard . This is very useful!
I have left a few minor comments inline.
dev/ipynb_to_gallery.py
Outdated
if output_file_name is None: | ||
output_file_name = input_file_name | ||
open(output_file_name.replace(".ipynb", ".py"), "w").write(python_file) | ||
else: | ||
if not output_file_name.endswith(".py"): | ||
output_file_name = output_file_name + ".py" | ||
open(output_file_name, "w").write(python_file) |
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.
if output_file_name is None: | |
output_file_name = input_file_name | |
open(output_file_name.replace(".ipynb", ".py"), "w").write(python_file) | |
else: | |
if not output_file_name.endswith(".py"): | |
output_file_name = output_file_name + ".py" | |
open(output_file_name, "w").write(python_file) | |
if output_file_name is None: | |
output_file_name = input_file_name.replace(".ipynb", ".py") | |
if not output_file_name.endswith(".py"): | |
output_file_name = output_file_name + ".py" | |
open(output_file_name, "w").write(python_file) |
less indentation is usually better
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.
Note that you could also use the gammapy.utils.script.make_path
function which allows you to use pathlib.Path
objects with e.g. Path.write_text(python_file)
or Path.with_suffix(".py")
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.
Yes it's true that I could have used that.
Now that it is written like this, do you see any reason why we should switch to Path
?
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
@registerrier, @Astro-Kirsty, suggestion implemented ! |
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.
Thanks @MRegeard.. From my side, nothing more to add
This PR is related to issue #4441.
It adds a script in the dev folder that allows to convert
jupyter notebook
in thesphinx-gallery
python script format. This is especially useful when creating a tutorial. First create it as a jupyter notebook, and them convert it into a python script when the tutorial is ready to be reviewed.This PR also adds a small section in the dev how-to documentation that explain how to use this script.
Since this script uses
pypandoc
, I also addedpypandoc
to the pip section of the environment file. This is not a big deal because we already havepandoc
in the environment file andpypandoc
is just a python wrapper forpandoc
.I put it in the pip section because it is not available for os-arm in the conda-forge repository.