Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I document the Jupyter Notebook Profile startup?

When I start up the Jupyter Notebook I've modified the ipython_config.py in my ipython profile to automatically load numpy as np:

c.InteractiveShellApp.exec_lines = [
    'import numpy as np',
    ]

This works great. When I start up a Notebook, in the first cell I can immediately call all of the numpy library via np.. However, if I'm sharing this Notebook via a gist or some other method, these imports are not explicitly shown. This is suboptimal as it makes clear reproducibility impossible.

My question: Is there a way that I could automatically populate the first cell of a new Notebook with the code that I'm importing? (Or some other similar way to document the imports that are occurring for the Notebook).

I'd be OK with removing the exec_lines option and pre-populating the code that I have to run myself or some other solution that gets at the main idea: clear reproducibility of the code that I'm initially importing in the Notebook.

Edit

A deleted answer that might be helpful to people landing here: I found jupyter_boilerplate which as an installable Notebook extension "Adds a customizable menu item to Jupyter (IPython) notebooks to insert boilerplate snippets of code" -- would allow one to easily create a starting code snippet that could be filled in.

Sidenote to MLavoie because "comments disabled on deleted / locked posts / reviews"

Yes, you are right that:

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review – MLavoie Jul 8 '16 at 17:27

But, you'll notice, that this is a widget to be installed, so there isn't relevant code to paste here. It was unhelpful to delete the above answer.

like image 357
JBWhitmore Avatar asked Apr 26 '15 19:04

JBWhitmore


People also ask

How do I change the Startup folder in Jupyter Notebook?

Change Jupyter Notebook startup folder (Windows) Copy the Jupyter Notebook launcher from the menu to the desktop. Right click on the new launcher and change the Target field , change %USERPROFILE% to the full path of the folder which will contain all the notebooks.

What is Jupyter Notebook default directory?

Configuration files Config files are stored by default in the ~/. jupyter directory.


1 Answers

Almost automatically:

%load startup.py

Put import/config code in a version controlled file on your PYTHONPATH and %load it into the first cell.

This has the advantage of allowing you to use different startup code without tweaking your startup config, and notebooks remain portable, i.e. send the notebook and startup file to other users and they can run it without tweaking their startup config.

like image 139
sp00n3r Avatar answered Oct 23 '22 02:10

sp00n3r