jupyter / jupyter/notebook

How do I configure jupyter notebook for having a pre loaded cell with certain imports without using ipython?

Open
#4,624 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
13.3k
Forks
5.8k
Avg merge
6d 11h
Merged PRs (30d)
7

Description

I want to import certain libraries and define certain functions in my first cell of the notebook every time it starts. In my case, I am using a Docker container for this purpose. After going through several links and answers(https://vivekbharadwaj.github.io/data%20science/jumpstart-your-Jupyter-notebook-analysis-with-pre-loaded-cells/), I am unable to set the notebook up as I have certain errors.

These are the commands:
```
# Python3 Kernel for JuPyter notebooks
RUN python3 -m pip install ipykernel
RUN python3 -m ipykernel install --user
RUN python3 -m pip install jupyter_contrib_nbextensions
RUN mkdir -p /usr/local/airflow/.local/share/jupyter/preload_import_code
RUN echo "define([ 'base/js/namespace' ], function( Jupyter ) { function load_ipython_extension() { if (Jupyter.notebook.get_cells().length===1){ //change this piece of code to what you want Jupyter.notebook.insert_cell_above('code', 0).set_text(\"import os, sys;\"); } } return { load_ipython_extension: load_ipython_extension }; });" >> /usr/local/airflow/.local/share/jupyter/preload_import_code/main.js
RUN jupyter nbextension enable --sys-prefix preload_import_code/main
```

With the current similar answers and questions, I am unable to have my configuration either due to error or because `ipython` is used.

While running, I get the following error on this step:
```
Step 36/41 : RUN jupyter nbextension enable --sys-prefix preload_import_code/main
---> Running in c56e28731e36
Enabling notebook extension preload_import_code/main...
- Validating: problems found:
- require? X preload_import_code/main
Removing intermediate container c56e28731e36
---> ea0c6505dd57
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Docker build commands and inspect the generated /usr/local/airflow/.local/share/jupyter/preload_import_code/main.js. Check the output from `jupyter nbextension enable --sys-prefix preload_import_code/main`, then verify that the extension validates and a new notebook opens with the requested first cell.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, jupyter-notebook, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.