Modularize Jupyter notebook preamble - python

I have a set of Jupyter notebooks, all of which need the same lines of code to initialize. The initialization contains:
IPython magic commands (such as %matplotlib inline and %load_ext autoreload)
importing modules
configuring some settings, such as plot style
I am looking for a good way to put this code into a module that can be imported and ideally called with something like:
import preamble
preamble.run()
For the initialization code it is easy to do, but what about the module imports and magic commands?

You may instead use a jupyter notebook for that.
Simply put all your code into preamble.ipynb and run it in your first cell with:
%run ./preamble.ipynb

Related

How to load Python script into Jupyter Notebook without revealing code and making it available to all cells

I'm trying to import a Python script as a module into one of my Notebooks, but the import or import importlib commands do not work in Jupyter Notebook like they do in a standard Python file and terminal.
I've come across the %load command but as far as I understand and have seen in my own Notebook, is that it only loads the contents of the script I'm trying to import into the current cell and is invisible to the other cells, and it also outputs the code in the script being imported for everyone to see.
I want to be able to load the script and make it available to all cells, as well as hiding the code for the purpose of encapsulation and also keeping the Notebook neat and tidy - only focusing on the code relevant to the topic of the Notebook. Is this possible?

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?
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.
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

Reloading python function

I am using a jupyter lab notebook and trying to modify code, reload it within the jupyter notebook and use the modified code without reloading the kernel. I am using python 3.5.5 and am running code like this:
(in file test.py)
def myTest():
print('hello')
(in jupyter)
from test import myTest
import importlib
importlib.reload(test)
myTest()
When I run the code in my jupyter lab notebook I get a NameError that name 'test' is not defined. From searching on stackoverflow the only references I find to this error is problems using older version of python. But the way I am using importlib.reload() seems to be correct.
Have you tried the built-in magic command autoreload?
At the beginning of your notebook, add:
%load_ext autoreload
%autoreload 2

Rerun import in Jupyter notebook

In jupyter notebook, I have a cell including all imports. Those imports some of my own modules.
When I rerun the cell, it does not pick up the updates in the modules. I need to to a kernel restart and rerun the whole notebook. Is there any other way to fix this problem?
You can also use the reload magic by placing this in your notebook. It will automatically reload code.
%reload_ext autoreload
%autoreload 2
The only time this may cause confusion is, if you instantiated an object, change the code and then wonder, why the already instantiated object does not have the new functions. Besides this case, it works well.
This question is also asked here.

Synchronize .ipynb and .py to use ipython notebook and eclipse at the same time

I started programming some scripts with ipython notebook but now the project is becoming to big for a notebook. Nevertheless I love to execute my stuff in an ipython notebook (load de data only once, online figures...).
What I would want is to program everything with eclipse but executing it in ipython. I know I can save the notebooks as .py by adding the --script option at the beginning. But now I want to automatically make the process the other way around. I mean, I want my ipython notebook to reload de code I modify with Eclipse.
Is it possible?
Should I make a program that makes it using the converter?
Thanks!!
I found the solution for manually updating the functions without rerunning the whole .ipynb file. However, I do not know how to make it automated. Here is the solution:
Synchronizing code between jupyter/iPython notebook script and class methods
To cut it short you need to put reload function from importlib module in the cell of interest:
import babs_visualizations
from importlib import reload
reload(babs_visualizations)
Just a little addition: make sure that you are addressing the function in the form of moldule.function. If you previously imported function by from module import function and then reloaded the module the function will not be reloaded. You can put the function inside the notebook cell and rerun it to see, how the changes in the module affected the function output in the notebook.
I hope this was helpful for someone.

Categories

Resources