nesting jupyter runs

Jupyter notebooks are a great way of sharing knowledge in science, art, programming. For instance, in a recent musing, I tried to programmatically determine the color of the sky. This renders as a web page, but is also a piece of runnable code.

As such, they are also great ways to store the knowledge that was acquired at a given time and that could be reusable. This may be considered as bad programming and may have downsides as described in that slides :

Joel Grus

Recently, thanks to an answer to a stack overflow question, I found a way to overcome this by detecting if the caall to a notebook is made from the notebook itself or from a parent.

It's as simple as this cell:

In [1]:
#verb =  (__name__ == "__main__")
def has_parent():
    """
    https://stackoverflow.com/questions/48067529/ipython-run-magic-n-switch-not-working
    
    Return True if this notebook is being run by calling
    %run in another notebook, False otherwise.
    """
    try:
        __file__
        # __file__ has been defined, so this notebook is 
        # being run in a parent notebook
        return True

    except NameError:
        # __file__ has not been defined, so this notebook is 
        # not being run in a parent notebook
        return False
def do_verb():
    return not has_parent()

verb = do_verb()
if verb : print('__name__=', __name__, '\nAm I a running this notebook directly? ', verb)
__name__= __main__ 
Am I a running this notebook directly?  True

Let's see what we have in the memory:

In [2]:
if verb:
    %whos
Variable     Type        Data/Info
----------------------------------
do_verb      function    <function do_verb at 0x1066ac360>
has_parent   function    <function has_parent at 0x1066ac040>
verb         bool        True

Let's run the above mentioned notebook by it from this URL:

In [3]:
if verb:
    %run -n 2020-07-04-colors-of-the-sky.ipynb
    verb = do_verb()

And now:

In [4]:
if verb:
    %whos
Variable         Type            Data/Info
------------------------------------------
CMF              ndarray         81x4: 324 elems, type `float64`, 2592 bytes
CMF_str          str             380 0.0014 0.0000 0.0065\<...>n780 0.0000 0.0000 0.0000
ColourSystem     type            <class '__main__.ColourSystem'>
bluesky          ndarray         3: 3 elems, type `float64`, 24 bytes
chappuis         function        <function chappuis at 0x11e9b74c0>
cs_srgb          ColourSystem    <__main__.ColourSystem object at 0x107344810>
do_verb          function        <function do_verb at 0x1066ac040>
fig_width        int             15
figsize          tuple           n=2
fontsize         int             20
has_parent       function        <function has_parent at 0x106bdafc0>
i                int             80
illuminant_D65   ndarray         3: 3 elems, type `float64`, 24 bytes
intensity5800    ndarray         81: 81 elems, type `float64`, 648 bytes
line             str             780 0.0000 0.0000 0.0000
np               module          <module 'numpy' from '/op<...>kages/numpy/__init__.py'>
os               module          <module 'os' (frozen)>
phi              float64         1.618033988749895
planck           function        <function planck at 0x11e9b7060>
plt              module          <module 'matplotlib.pyplo<...>es/matplotlib/pyplot.py'>
rcParams         RcParams        _internal.classic_mode: F<...>: 0.6\nytick.right: False
scatter          ndarray         81: 81 elems, type `float64`, 648 bytes
scattering       function        <function scattering at 0x11e9b7240>
spectrum         ndarray         81: 81 elems, type `float64`, 648 bytes
verb             bool            True
wavelengths      ndarray         81: 81 elems, type `float64`, 648 bytes
xyz_from_xy      function        <function xyz_from_xy at 0x11e9b7600>

Meaning that I can access the functions and some data from this notebook, to be used in another one.

In [5]:
if verb:
    print('The color of the sky is equal to ', bluesky)
The color of the sky is equal to  [0.488779 0.672615 1.      ]

some book keeping for the notebook

In [6]:
if verb:
    %load_ext watermark
    %watermark -i -h -m -v -r -g -b
    
Python implementation: CPython
Python version       : 3.11.6
IPython version      : 8.17.2

Compiler    : Clang 15.0.0 (clang-1500.0.40.1)
OS          : Darwin
Release     : 23.1.0
Machine     : arm64
Processor   : arm
CPU cores   : 10
Architecture: 64bit

Hostname: obiwan.local

Git hash: 3f1c25c7a1ca749e735fb886b056f7f2e665d77c

Git repo: https://github.com/laurentperrinet/sciblog.git

Git branch: master