jupyter and common functions

399 views
Skip to first unread message

zlju...@gmail.com

unread,
Oct 31, 2018, 5:02:29 AM10/31/18
to PyData
Hi,

I would like to have one jupyter(lab) notebook with shared functions located anywhere in the file system.
Let's say that this notebook's name is my_func and in it there are two functions: func1 and func2

If I now create a new notebook, how to access functions from my_func notebook?

I found a solution using https://github.com/grst/nbimporter, but this solution has a prerequisite that shared notebook (my_func) should be at the same (sub)dir as a new notebook.
I am trying to find out a solution in which my_func notebook can be anywhere in the file system, regardless of location of new notebook.

Does someone have an experience with reusing shared notebooks?

Regards.

Pietro Battiston

unread,
Oct 31, 2018, 5:50:40 AM10/31/18
to pyd...@googlegroups.com
Il giorno mer, 31/10/2018 alle 02.02 -0700, zlju...@gmail.com ha
scritto:
> [...]
>
> I found a solution using https://github.com/grst/nbimporter, but this
> solution has a prerequisite that shared notebook (my_func) should be
> at the same (sub)dir as a new notebook.
> I am trying to find out a solution in which my_func notebook can be
> anywhere in the file system, regardless of location of new notebook.
>
> Does someone have an experience with reusing shared notebooks?


Although I would use this only in exceptional circumstances (functions
used by multiple notebooks or even multiple independent projects are
better stored in separate .py modules, which by the way can still be
edited through the jupyter interface), it should be perfectly feasible
to put nbimporter imports after

sys.path.append('dir/where/the/nb/is')

Pietro

zlju...@gmail.com

unread,
Oct 31, 2018, 8:41:18 AM10/31/18
to PyData
Pietro,

so you want to say that I can have jupyter notebook or *.py file in sys.path.append('dir/where/the/nb/is')
and than for *.py import python_file.py
or for yupiter notebook
import nbimporter
from jupyter_notebook import func
?

Regards.

Pietro Battiston

unread,
Oct 31, 2018, 9:25:11 AM10/31/18
to pyd...@googlegroups.com
Il giorno mer, 31/10/2018 alle 05.41 -0700, zlju...@gmail.com ha
scritto:
> Pietro,
>
> so you want to say that I can have jupyter notebook or *.py file in
> sys.path.append('dir/where/the/nb/is') 

I don't understand this part, but maybe your question is answered by
https://docs.python.org/3/tutorial/modules.html#standard-modules

Pietro

Sal Abbasi

unread,
Oct 31, 2018, 11:56:44 AM10/31/18
to pyd...@googlegroups.com
Here’s how I do this:

1.  I automatically write my notebooks as a .py file every time I save them in jupyter.  To do this you have to create a jupyter_notebook_config.py file with the contents below and put it into your ~/.jupyter folder
2.  I add the folder where I save .py files into sys.path using the following lines in my ~/.ipython/profile_default/ipython_config.py file.

Don’t forget to add an empty __init__.py file to the directory you want to use as the root for your packages

Addition to end of ipython_config.py file

c.InteractiveShellApp.exec_lines = ['import sys; import os; home =  os.path.expanduser("~"); sys.path.append(home + “[folder name]");’]   # replace folder name with your root folder for packages

jupyter_notebook_config.py file:

# Based off https://github.com/jupyter/notebook/blob/master/docs/source/extending/savehooks.rst

import io
import os
from notebook.utils import to_api_path


def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    import sys,json

     f = open(os_path, 'r') #input.ipynb
     j = json.load(f)
    output_fname = base + '.py'
    log.info("Saving script /%s", to_api_path(output_fname, contents_manager.root_dir))
    of = open(output_fname, 'w') #output.py
    for i,cell in enumerate(j["cells"]):
            if cell["cell_type"] != "code": continue
            of.write("#cell "+str(i)+"\n")
            for line in cell["source"]:
                of.write(line)
            of.write('\n\n')
    of.close()

     if os.path.isfile(base + '.pyc'):
        os.remove(base + '.pyc')
     log.info("Done saving script /%s", to_api_path(output_fname, contents_manager.root_dir))

c.FileContentsManager.post_save_hook = script_post_save



Best,

Sal




--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zlju...@gmail.com

unread,
Nov 5, 2018, 7:03:30 AM11/5/18
to PyData
Very interesting approach Sal. Thanks.
Reply all
Reply to author
Forward
0 new messages