Zane Dufour
Zane Dufour

Reputation: 910

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:

-- startup.py --

sc = "This is a spark connection"

I want to be able to have that code snippet run every time I open a kernel. I've found some stuff about the Jupyter Configuration File, but it doesn't seem like variables defined there show up when I try to run

print(sc)

in a notebook. Is there a command-line option that I could use -- something like:

jupyter notebook --startup-script startup.py

or do I have to include something like

from startup import sc, sqlContext

in all of the notebooks where I want those variables to be defined?

Upvotes: 6

Views: 4314

Answers (2)

Tomas
Tomas

Reputation: 4196

A custom package or explicit loading is not needed (though might be preferred if you work with others): you can have auto-executed startup scripts šŸ‘‰ https://stackoverflow.com/a/47051758/2611913

Upvotes: 1

Daniel Lenz
Daniel Lenz

Reputation: 3857

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.

Upvotes: 3

Related Questions