Skip to content Skip to sidebar Skip to footer

How To Run Python Code Before Every Jupyter Notebook Kernel

Suppose I have a code snippet that I'd like to run every time I open a jupyter notebook (in my case it's opening up a Spark connection). Let's say I save that code in a .py script:

Solution 1:

I'd recommend to create a startup file as you suggested, and include it via

%load ~/.jupyter/startup.py

This will paste the content of the file into the cell, which you can then execute.

Alternatively, you can write a minimal, installable package that contains all your startup code.

Pro: Doesn't clutter your notebook

Con: More difficult to make small changes.

Post a Comment for "How To Run Python Code Before Every Jupyter Notebook Kernel"