Set up the first default cell in Jupyter laptops

TI is there a way to configure the first default cell for a particular python kernel in a Jupyter laptop? I agree that importing python by default goes against good coding practice.

So, is it possible to configure the laptop so that the first cell of the new python laptop is always

import numpy as np

eg?

+9
source share
5 answers

Creating an IPython profile, as mentioned above, is a good first solution, but IMO is not completely satisfying, especially when it comes to code sharing.

, exec_lines, , . / .

Jupyter, . . nbextension Jupyter. : https://github.com/ipython-contrib/jupyter_contrib_nbextensions pip: pip install jupyter_contrib_nbextensions

nb, "default_cells" , nb. , Ubuntu /usr/local/share/jupyter/nbextensions/., , : C:\Users\xxx.xxx\AppData\Roaming\jupyter\nbextensions\

3 :

  • main.js, js
  • default_cells.yaml API Jupyter
  • README.MD , API.

: https://github.com/jupyter/notebook/issues/1451my main.js:

define([
    'base/js/namespace'
], function(
    Jupyter
) {
    function load_ipython_extension() {
      if (Jupyter.notebook.get_cells().length===1){
   //do your thing
        Jupyter.notebook.insert_cell_above('code', 0).set_text("# Scientific libraries\nimport numpy as np\nimport scipy\n\n# import Pandas\n\nimport pandas as pd\n\n# Graphic libraries\n\nimport matplotlib as plt\n%matplotlib inline\nimport seaborn as sns\nfrom plotly.offline import init_notebook_mode, iplot, download_plotlyjs\ninit_notebook_mode()\nimport plotly.graph_objs as go\n\n# Extra options \n\npd.options.display.max_rows = 10\npd.set_option('max_columns', 50)\nsns.set(style='ticks', context='talk')\n\n# Creating alias for magic commands\n%alias_magic t time");
      }
    }
    return {
        load_ipython_extension: load_ipython_extension
    };
});

..yaml :

Type: IPython Notebook Extension
Compatibility: 3.x, 4.x
Name: Default cells
Main: main.js
Link: README.md
Description: |
  Add a default cell for each new notebook. Useful when you import always the same libraries$


Parameters:
- none

README.md

default_cells
=========

Add default cells to each new notebook. You have to modify this line in the main.js file to change your default cell. For example

`Jupyter.notebook.insert_cell_above('code', 0).set_text("import numpy as np/nimportpandas as pd")`


You can also add another default cell by creating a new line just below :

`Jupyter.notebook.insert_cell_above('code', 1).set_text("from sklearn.meatrics import mean_squared_error")`

**Don't forget to increment 1 if you want more than one extra cell. **

" " "nbextensions", Jupyter.

, , , . , .

+4

: %load .

firstcell.py:

%reload_ext autoreload
%autoreload 2

import numpy as np
import pandas as pd
... 

%load firstcell.py , jupyter

# %load firstcell.py
%reload_ext autoreload
%autoreload 2

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

import, , , , .

+4

:

~/.ipython/profile_default/startup/

README:

IPython

.py .ipy , exec_lines exec_files , .

, , .::

00-first.py
50-middle.py
99-last.ipy

, 00_imports.py, :

import numpy as np

, %matplotlib inline use .ipy, .

, , notebook extension, , , . github : https://github.com/jupyter/notebook/issues/640

0

, , - % load .

Python __JN_init.py

import numpy as np

, , :

%load __JN_init.py

. . , .

0

:

1. , .jupyternotebookrc:

# ~/.ipython/profile_default/startup/run_jupyternotebookrc.py

import os
import sys

if 'ipykernel' in sys.modules: # hackish way to check whether it running a notebook
    path = os.getcwd()
    while path != "/" and ".jupyternotebookrc" not in os.listdir(path):
        path = os.path.abspath(path + "/../")
    full_path = os.path.join(path, ".jupyternotebookrc")
    if os.path.exists(full_path):
        get_ipython().run_cell(open(full_path).read(), store_history=False)

2 - , :

# .jupyternotebookrc in any folder or parent folder of the notebook

%load_ext autoreload
%autoreload 2
%matplotlib inline

import numpy as np

.jupyternotebookrc , , .

0
source

All Articles