Magic commands in Jupyter notebooks are a great addition to my workflow. One really useful magic command comes with the ipyext package.
"Writeandexecute" allows you to quickly save classes or functions in a Jupyter notebook as separate *.py files. No need to copy and paste code, just two lines of magic commands and the file is saved and can be reused via import.
For this magic command to work you will need to install the ipyext package first.
- Install ipyext:
conda install -c https://conda.anaconda.org/janschulz ipyext
pip install ipyext
- Load extension in Jupyter
- Save module to file
Put the following code on top of the cell with your function or class you want to export.
%%writeandexecute -i identifier filename.py
%%writeandexecute -i user_accounts user.py
- Run the cell to write the file.
- Now you are ready to use the module
Follow the link to find the full documentation of the ipyext package.