<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introduction
Step1:  cells
Step2:  What happens if it's invalid?
Step3:  Cells and their sources
Step4:  Three types of cells
Step5:  cell_type
Step6:  cell_type
Step7:  cell_type
<ASSISTANT_TASK:>
Python Code:
import nbformat
from nbformat.v4 import new_notebook
nb = new_notebook()
display(nb)
Explanation: Introduction: *.ipynb and nbformat
*.ipynb
JSON on-disk representation
json schema defines required structure
current schema version: 4
nbformat
Python library for simple notebooks operations.
Straightforward questions: 
Minimal structure needed to meet the schema?
Validate a notebook against the schema?
Minimal structure
End of explanation
nbformat.validate(nb)
Explanation: cells: list
metadata: dict
nbformat, nbformat_minor: int, int
Validate
End of explanation
nb.pizza = True
nbformat.validate(nb)
Explanation: What happens if it's invalid?
End of explanation
nb = new_notebook() # get rid of pizza
from nbformat.v4 import new_code_cell, new_markdown_cell, new_raw_cell
Explanation: Cells and their sources
End of explanation
md = new_markdown_cell("First argument is the source string.")
display(md)
nb.cells.append(md)
Explanation: Three types of cells:
code_cell
markdown_cell
raw_cell
Markdown cells
End of explanation
raw = new_raw_cell(["Sources can also be a ","list of strings."])
display(raw)
nb.cells.append(raw)
Explanation: cell_type: str, "markdown"
metadata: dict
source: str or list of strings
Raw cells
End of explanation
code = new_code_cell(["#Either way, you need newlines\n", 
                      "print('like this')"])
display(code)
nb.cells.append(code)
Explanation: cell_type: str, "raw"
metadata: dict
source: str or list of strings
Code cells
End of explanation
nbformat.write(nb, "my_demo_notebook.ipynb")
!ls my_*
nb2 = nbformat.read("my_demo_notebook.ipynb", as_version=4)
print(nb2)
Explanation: cell_type: str, "code"
execution_count: None or int
metadata: dict
outputs: list
source: str or list of strings
Creating outputs
Output types:
stream
display_data
execute_result
error
display_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
Metadata
notebook-level, nb.metadata
cell-level, nb.cells[0].metadata
output_level (for display_data and execute_result types), nb.cells[0].outputs[0].metadata
Arbitrary content, with some reserved Jupyter specific fields.
Reserved notebook metadata fields:
kernelspec
language_info
authors 
title
Reserved cell metadata fields (all are optional):
deletable
collapsed
autoscroll 
jupyter (jupyter metadata namespace, for internal use)
tags (useful for semantic customization)
name (should be unique)  
for raw cells: format (content type)
Reserved output metadata
isolated
display_data and execute_result metadata are keyed with mimetypes.
Reading & writing notebooks to disk
End of explanation
<END_TASK>