NEUBIAS / training-resources

Resources for teaching/preparing to teach bioimage analysis
https://neubias.github.io/training-resources
Other
45 stars 21 forks source link

Interconversion python code and jupyter notebook #515

Open manerotoni opened 1 year ago

manerotoni commented 1 year ago

It would be very nice to have a plain text python file that correctly configures as a jupyter notebook. This is indeed possible.

There are different ways to do this https://mljar.com/blog/convert-jupyter-notebook-python/

I tried with the package jupytext pip install jupytext or conda install jupytext -c conda-forge

You then define in the .py file each beginning of a cell with # %%. This also the way to create cells in some IDEs Loading the file in jupyter with an environment that has jupytext will correctly configure the cells.

If you need a markdown cell use

# %% [markdown]
# Example notebook
tischi commented 1 year ago

Nice! Maybe we should then consider structuring our python code into such sections!

manerotoni commented 1 year ago

Yes, all new activities I am editing I restructure like this. What would be nice to have, would be a download function for the activity code. Then using the jupytext package participants get a nicely formatted jupyter notebook. Alternatively they copy paste in a text editor and load the activity into jupyter. I would add jupytext as extra pre-requisite to install. It will be very useful. What do you think @tischi ?

tischi commented 1 year ago

Can one somehow copy and paste one section into Jupyter such that it then renders two cells: a comment cell and a code cell?

manerotoni commented 1 year ago

Unfortunately no. You have to upload a .py document and it will be transformed

tischi commented 1 year ago

OK, yes, we could add instructions somewhere for how to load our .py files nicely formatted into Jupyter. Question is where... maybe in the tool installation module?

manerotoni commented 1 year ago

Tool installation module. Together with the additional package to install. Are you editing the tool installation part? If yes you can make those changes pip install jupytext and some explanations on how to use the activities. It could be part of the first run to see if the software works.

manerotoni commented 1 year ago

Ok I see you have a PR, I will add these changes in it during the review process.

manerotoni commented 1 year ago

To make openIJTIff always available in your environment add the path to your Pythonpath. On windows and for my installation it is set PYTHONPATH=D:\Code\training-resources\functions This you have to do before you start jupyter notebook.

Disable the autosave function to avoid confusion (File > Jupytext > Autosave notebook (uncheck it))