Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modularize Jupyter notebook preamble

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?

like image 517
clstaudt Avatar asked Oct 21 '25 18:10

clstaudt


1 Answers

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
like image 147
Bicudo Avatar answered Oct 24 '25 07:10

Bicudo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!