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){
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.
, , , . , .