Parente's Mindtrove

Jupyter Tidbit: %run your ipynb file

August 21, 2018

This post originates from a gist that supports comments, forks, and execution in binder.


The %run magic provided by IPython not only supports the execution of regular Python scripts, it also runs Jupyter Notebook files (.ipynb).



The run_demo.ipynb notebook below uses %run to execute all of the cells in reusable_stuff.ipynb. Once it does, both globals defined in reusable_stuff are available in run_demo for use.

Why is this useful?

You can maintain a handy notebook of useful recipes that you can than %run to reuse in other notebooks. Just remember that this setup can decrease the reproducibility of your work unless you provide your recipe notebook alongside any notebook that uses it when you share.


In [1]:
def super_useful_thing():
    return 'not really'
In [2]:
x = 'from reusable stuff'


In [10]:
x = 1

Execute the cells in reusable_stuff.ipynb in the current kernel namespace.

In [11]:
%run reusable_stuff.ipynb
In [12]:
'not really'
In [13]:
'from reusable stuff'

Another Read: Flatten Nested JSON with Pandas »

I believe the pandas library takes the expression "batteries included" to a whole new level (in a good way). Recent evidence: the function. It turns an array of nested JSON objects into a flat DataFrame with dotted-namespace column names. It may not seem like much, but I've found it invaluable when working with responses from RESTful APIs.