mesh-adaptation / mesh-adaptation-docs

Documentation source for Animate, Goalie, and Movement
MIT License
1 stars 0 forks source link

Converting demos from py to ipynb #34

Open ddundo opened 1 month ago

ddundo commented 1 month ago

Further to our discussion this morning, I converted animate's simple_metric.py demo to a format suitable for converting it to a Jupyter notebook using jupytext. This is very simple - I just had to add # %% [markdown] before text blocks and # %% before code blocks. This is actually standard formatting - I noticed now that VS code automatically recognised it in the .py script and asked me to run each of these partition interactively. So that's nice too :)

So my idea was to keep demos in this .py format and then only convert them to notebooks when building the website. This can be easily done in command line or in a python script, e.g.:

import jupytext

nb = jupytext.read('simple_metric.py')
jupytext.write(nb, 'simple_metric.ipynb', fmt='.ipynb')

and then the notebook can be executed with jupytext --execute simple_metric.ipynb which runs the code and produces figures in the notebook, etc.

Here is the demo script if you'd like to test it out: https://gist.github.com/ddundo/32fc425e0e1de246b06b6cf09d903818

ddundo commented 4 weeks ago

Actually, the # %% [markdown] labels aren't necessary and neither are the code cell labels # %%. Although without the code labels, each empty line in the .py script would mean end of the cell. So e.g.

a = 1

b = 2

would be 2 cells in the notebook. Maybe this could be modified in the jupytext config... not sure.

There are also some jupytext settings for sphinx/rst compatibility. I didn't test this out. But potentially we could keep the demos exactly as they are.

I can test all of this out if we decide to go with the notebooks idea :)

jwallwork23 commented 3 weeks ago

Sounds good, thanks! Happy for you to do this.