grst / nbimporter

Import ipython notebooks as modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Re-importing notebook

cmrfrd opened this issue · comments

Currently the workflow described in the README can't reload a notebook and requires a restarted ipython kernel to re import any changes from a notebook

Current

Notebook foo.ipynb:
In[1]:

def a(): 
    print("Hello World!")

Notebook bar.ipynb:

In[1]:

import nbimporter
from foo import a #import from notebook

a()

Out[1]:

Hello World!

Change

Notebook foo.ipynb:

def a(): 
    print("Hello To The World!")

When rerunning in Notebook bar.ipynb:

In[1]:

import nbimporter
from foo import a #import from notebook

a()

The same Hello World! output is displayed. I have tried using %autoreload magic and hoping for the best but that ended up with no luck. I also tried removing the NotebookFinder object from sys.meta_path and re importing with no success.

Should it be possible to reload a notebook on every new cell execution or is there a better way? Maybe a
In[1]:

import nbimporter
import foo

In[2]:

nbimporter.reload(foo) ## Reload the notebook

Thanks for your input!
What currently works is the following:

from nbimporter import NotebookLoader
loader = NotebookLoader()
a = loader.load_module("foo").a
a()
>> Hello To The World!

calling a = loader.load_module("foo").a again will re-import the altered function.

However it would be nice if we could somehow manage to get this work with autoreload.