Open kenjioman opened 4 years ago
Hello @kenjioman , this is a great question!
We say a word about that at https://jupytext.readthedocs.io/en/latest/examples.html#importing-jupyter-notebooks-as-modules. Currently the paragraph is very short and I'll be happy to improve it based on your feedback.
So, you have the option to 'tag' your code cells with active-ipynb
if you want them to be active only in Jupyter, and tag raw cells with active-py
if you want them to be active only in the script.
Now, if you're open to changing the notebook itself and to only write functions, then I suppose you could code it as in your example, and finish with a code cell with a tag active-ipynb
that simply calls main()
- what do you think?
@kenjioman I actually have a hack does make them portable and testable! In two different ways:
@mwouts Maybe we could merge this testing hack in JuPyText as a utility?
This notebook will run as an iPython notebook while still being importable. https://github.com/facebookresearch/habitat-sim/blob/master/examples/tutorials/nb_python/rigid_object_tutorial.py
Now if you want to run the entire file as a function, I actually do that in our testing suite: https://github.com/facebookresearch/habitat-sim/blob/master/tests/test_examples.py
Sorry in advance if this feature already exists and I just don't know about it, or if there are easy work-arounds to get the same behavior.
Taking a look at the generated python files in the demo, it looks like everything in the notebooks are kept at global scope in the python file. So, if these were to be imported by any other files, everything would be run on import.
Instead, it would be nice if generated python files (or, at least one variant of them) followed this structure:
Doing this would:
import genrated_script
andgenerated_script.main()
.