5

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?

Zane Dufour
  • 830
  • 1
  • 9
  • 19
  • Possible duplicate of [Start ipython running a script](https://stackoverflow.com/questions/3323230/start-ipython-running-a-script) – ivan_pozdeev Aug 18 '18 at 10:03

2 Answers2

2

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.

Daniel Lenz
  • 3,334
  • 17
  • 36
0

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

Tomas
  • 3,086
  • 2
  • 15
  • 8