Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create/modify a jupyter notebook from code (python)?

I am trying to automate my project create process and would like as part of it to create a new jupyter notebook and populate it with some cells and content that I usually have in every notebook (i.e., imports, titles, etc.)

Is it possible to do this via python?

like image 824
oshi2016 Avatar asked Jul 05 '16 01:07

oshi2016


People also ask

How do I edit a notebook in Jupyter?

Edit mode. When a cell is in edit mode, you can type into the cell, like a normal text editor. Enter edit mode by pressing Enter or using the mouse to click on a cell's editor area.

How do you code a Python in Jupyter Notebook?

Inside the Notebook. When you open a new Jupyter notebook, you'll notice that it contains a cell. Cells are how notebooks are structured and are the areas where you write your code. To run a piece of code, click on the cell to select it, then press SHIFT+ENTER or press the play button in the toolbar above.

Can I customize Jupyter Notebook?

Jupyter Notebook is a great programming environment and often the most popular choice for data scientists or data analysts that are coding in python. Unfortunately, its default settings do not allow the level of customization that you have with standard programming environments such as PyCharm or similar tools.


1 Answers

You can do it using nbformat. Below an example taken from Creating an IPython Notebook programatically:

import nbformat as nbf

nb = nbf.v4.new_notebook()
text = """\
# My first automatic Jupyter Notebook
This is an auto-generated notebook."""

code = """\
%pylab inline
hist(normal(size=2000), bins=50);"""

nb['cells'] = [nbf.v4.new_markdown_cell(text),
               nbf.v4.new_code_cell(code)]
fname = 'test.ipynb'

with open(fname, 'w') as f:
    nbf.write(nb, f)
like image 124
Duccio A Avatar answered Oct 13 '22 11:10

Duccio A