Straightforward questions:
In [ ]:
import nbformat
from nbformat.v4 import new_notebook
nb = new_notebook()
display(nb)
cells
: listmetadata
: dictnbformat
, nbformat_minor
: int, intValidate
In [ ]:
nbformat.validate(nb)
What happens if it's invalid?
In [ ]:
nb.pizza = True
nbformat.validate(nb)
In [ ]:
nb = new_notebook() # get rid of pizza
from nbformat.v4 import new_code_cell, new_markdown_cell, new_raw_cell
In [ ]:
md = new_markdown_cell("First argument is the source string.")
display(md)
nb.cells.append(md)
cell_type
: str, "markdown"metadata
: dictsource
: str or list of strings
In [ ]:
raw = new_raw_cell(["Sources can also be a ","list of strings."])
display(raw)
nb.cells.append(raw)
cell_type
: str, "raw"metadata
: dictsource
: str or list of strings
In [ ]:
code = new_code_cell(["#Either way, you need newlines\n",
"print('like this')"])
display(code)
nb.cells.append(code)
cell_type
: str, "code"execution_count
: None
or intmetadata
: dictoutputs
: listsource
: str or list of stringsdisplay_data
and execute_result
can have multiple mimetypes.
For more on messages and output types:
Matthias Bussonier and Paul Ivanov's
Jupyter: Kernels, protocols, and the IPython reference implementation
In [ ]:
nbformat.write(nb, "my_demo_notebook.ipynb")
!ls my_*
In [ ]:
nb2 = nbformat.read("my_demo_notebook.ipynb", as_version=4)
print(nb2)