Search code examples
pythonclassoopjupyter-notebookjupyter

Jupyter Class in different notebook?


i am currently forced in an project to work with jupyter notebook, before I just used PyCharm. So I create a project and would like to add some classes to it. Should I write all of them in the one main notebook or create different notebooks for each class? As far as I understand, jupyter is not made for using several notebooks for one project (but I thnink there are options) but if I want to use some of my classes more general for other projects it does not make sense to not have it in a seperate notebook right?

I am sorry, maybe I have a basic misunderstanding of jupyter but I could not figure out yet, what is the best way.


Solution

  • Jupyter will happily allow you to use multiple libraries in other notebooks. You can find the documents here
    Though if you want to move the project out to a command line executable, you may want to consider exporting the notebooks as .py files and then importing them as standard python libraries.

    EDIT: November, 2024

    JupyText is a more robust way of converting .ipynb documents to .py files for use in other notebooks. Jupytext will also allow you to open .py files as if they were notebooks, though it sometimes bodges this pretty badly.

    Jupytext can be used as a stand-alone, on demand conversion, or to continually convert notebooks into .py files. You'll need a jupytext.toml file in the root of your project directory for live conversions.

    If you choose for continual conversion, I would strongly encourage you to make sure you have some sort of version control in place to make sure you don't clobber working .py files with a poorly thought-out edit to a notebook.

    It is possible to pair/unpair specific files as needed. See the manual.

    For best results, that excludes markdown and other jupyter notebook meta data, the light format is probably the best choice. YMMV.

    Sample jupytext.toml:

    # convert notebook <--> pure python
    formats = "ipynb,py:light"
    

    Notes:

    • Jupytext needs to be installed into each virtual environment you where you intend to use it
    • Jupytext tends to corrupt hash-bang lines (#! /usr/bin/env python). Use jupytext with care on your main script!

    Previous Answer

    Jupyter has a nbconvert method for this.

    I do this a lot and ended up writing a small script that adds appropriate hashbang lines and strips out any Jupyter "magic" commands. You can find it at github.

    Usage: ./nbconvert myNotebook.ipynb

    There are also some excellent Jupyter magic commands that make working with libraries much easier.

    # cause chagnged modules to be reloaded at execution
    # add this to any notebook that sources an external .py file
    %load_ext autoreload
    %autoreload 2
    

    I also find this useful:

    # set %magic alias to convert the current notebook into a .py file
    %alias nbconvert ~/devtools/nbconvert thisNotebook.ipynb
    

    Usage:

    %nbconvert
    [NbConvertApp] Converting notebook searchTools.ipynb to python