Selectively import from another Jupyter Notebook

2024/9/23 10:28:51

I arranged my Jupyter notebooks into: data.ipynb, methods.ipynb and results.ipynb. How can I selectively import cells from data and methods notebooks for use in the results notebook?

I know of nbimporter and ipynb but neither of those offers selective import of variables. There is an option to import definitions - including variables that are uppercase - but this does not work for me as I would have to convert most of the variables in my notebooks to uppercase.

I would rather import everything except for two or three cells that take a long time to evaluate. Ideally, I would like to defer the execution of some assignments to the very moment I access them (lazy evaluation) - but I understand that it might be difficult to implement.

Here is the overview, in pseudocode (each line repesents a cell):

data.ipynb:

raw_data = load_data()
dataset = munge(raw_data)
describe(dataset)             # I want this line to be skipped at import

methods.ipynb:

import data
method = lambda x: x * x
# showcase how the method works on a subset of the dataset
method(data.dataset[:5])      # I want this line to be skipped at import

results.ipynb:

import data
import methods
result = methods.method(data.dataset)
describe(result)

The motivation is that my real data and methods notebooks:

  • are way much longer and complicated, hence I want to use an import system
  • there are only a couple of cells that take more than seconds to evaluate

also, the methods notebook cannot be replaced with methods.py file. In fact, I have such a file which contains the implementation details of my method. The notebook is more of a place to specify default parameters, showcase how my method works and explain example results.

This question is essentially a combination of:

  • How can I import from another ipython-notebook?, and
  • Simple way to choose which cells to run in ipython notebook during run all

I read through answers to both and none satisfied my requirements.

In my answer below I present my solution that uses custom cell magics and monkey-patching. However, I would prefer a solution which allows specifying which cells/expressions to exclude/include not in the notebook of origin (e.g. data.ipynb) but in the target one (e.g. in methods.ipynb).

For example, it could use regular expressions:

# all variables starting with 'result' would be ignored
nbimporter.options['exclude'] = '^result.*'

or (even better) lazy evaluation:

# only `a` and `b` would be evaluated and imported
from data import a, b

All ideas will be appreciated!

Answer

So far I've been monkey-patching nbimporter and selecting cells to exclude using cell magic:

from IPython.core import magic@magic.register_cell_magic
def skip_on_import(args, cell):get_ipython().ex(cell)

The code used to monkey-patch of cell remover:

import astclass SkippingTransformer(ast.NodeTransformer):# usage:# import nbimporter # nbimporter.CellDeleter = SkippingTransformerdef visit(self, node):if (isinstance(node, ast.Expr)and isinstance(node.value, ast.Call)and isinstance(node.value.func, ast.Attribute)and node.value.func.attr == 'run_cell_magic'and node.value.args[0].s == 'skip_on_import'):returnreturn node

And an actual example, data.ipynb:

data.ipynb

And methods.ipynb (the exception at the end is intended - it means success!):

methods.ipynb

Edit: I published the above code as a part of jupyter-helpers some time ago. Using this package one simply needs to import the importer in the importing notebook:

from jupyter_helpers.selective_import import notebooks_importer 

and the cell-magic can be imported in the imported notebook with:

from jupyter_helpers.selective_import import skip_on_import

Here is example imported notebook: Data.ipynb and example importing notebook: Results.ipynb

https://en.xdnf.cn/q/71833.html

Related Q&A

supervisord event listener

Im trying to configure an event listener for supervisord but cant get it to work. I just want to listen for PROCESS_STATE changes and run some python code triggering an urllib2request.In my .conf I hav…

Integration of Java and Python Code in One Eclipse Project

I am writing a compiler in Python using Eclipse with PyDev. Ive come to a stage where I needed to write some code in Java. Im wandering if there is a way of combining these into a single project, bec…

formatting of timestamp on x-axis

Im trying to format the x-axis in my weather data plot. Im happy with the y-axis but all my tries to get the x-axis into a decent, human-readable format didnt work so far. So after several hours of tri…

How can I set the row height in Tkinter TreeView?

I wrote a small app recently that needs to be cross-platform. I used Python and Tkinter for the GUI.It works great but recently I got a new laptop with a hiDPI screen and it seems to mess up the TreeVi…

Is replace row-wise and will overwrite the value within the dict twice?

Assuming I have following data set lst = [u, v, w, x, y] lst_rev = list(reversed(lst)) dct = dict(zip(lst, lst_rev))df = pd.DataFrame({A:[a, b, a, c, a],B:lst},dtype=category)Now I want to replace the …

Python requests, how to add content-type to multipart/form-data request

I Use python requests to upload a file with PUT method.The remote API Accept any request only if the body contains an attribute Content-Type:i mage/png not as Request Header When i use python requests…

Django Year/Month based posts archive

im new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this http://www.flickr.com/photos/ion…

ValueError: You are trying to load a weight file containing 6 layers into a model with 0

I have a simple keras model. After the model is saved. I am unable to load the model. This is the error I get after instantiating the model and trying to load weights:Using TensorFlow backend. Tracebac…

cProfile with imports

Im currently in the process of learn how to use cProfile and I have a few doubts.Im currently trying to profile the following script:import timedef fast():print("Fast!")def slow():time.sleep(…

Python AWS Lambda 301 redirect

I have a lambda handler written in Python and I wish to perform a 301 redirect to the browser. I have no idea how I can configure the response Location header (or the status code) -- the documentation …