In this quick tutorial, we're going to see how to programmatically create a Jupyter Notebook. At the end you will know how to convert markdown, CSV or JSON to Jupyter Notebook.
Generate Notebook with Nbformat
To create a new notebook automatically by nbformat we can use library nbformat
. The library can be installed by:
pip install nbformat
To create new notebook we can use the following example:
import nbformat
from nbformat.v4 import new_notebook, new_markdown_cell, new_code_cell
nb = new_notebook() # create a new empty notebook
# add a markdown cell
cell1 = new_markdown_cell('Markdown cell.')
nb['cells'].append(cell1)
# add a code cell
cell2 = new_code_cell('print("code cell.")')
nb['cells'].append(cell2)
# save the notebook to a file
with open('new_notebook.ipynb', 'w') as f:
nbformat.write(nb, f)
Where:
new_notebook()
- create new notebooknew_markdown_cell('Markdown cell.')
- add new markdown cellnew_code_cell('print("code cell.")')
- add code cells
Notebook from markdown, CSV, JSON
We can use pandas to parse data stored as markdown, CSV JSON and create a new jupyter notebook. To do so we can:
- read the data with Pandas
- then iterate over the dataframe
- finally generate the notebook
Example is shown below:
import nbformat
from nbformat.v4 import new_notebook, new_markdown_cell, new_code_cell
nb = new_notebook()
name = '0_julia.ipynb'
for x, cat in enumerate(df.category.unique()):
print(cat)
cat_sum = cat_sum_img[cat_sum_img['category'] == cat]['category summary'].iloc[0]
image = cat_sum_img[cat_sum_img['category'] == cat]['image'].iloc[0]
if '\n' in cat_sum:
cat_sum = cat_sum.replace('\n', '\\n').replace('\t', '\\t')
cell1 = new_markdown_cell(f'## {x}.' + cat_sum)
nb['cells'].append(cell1)
for i, row in df[df.category == cat].reset_index().iterrows():
description = row.description
code = row.code2
if '\"' in code:
# print(code)
code = code.replace('\\"', '"')
# print(code)
msg = '# ' + description + '\n' + code
cell2 = new_code_cell(msg)
nb['cells'].append(cell2)
with open(name, 'w') as f:
nbformat.write(nb, f)
where data is:
# cat | category | code | code2 | |
---|---|---|---|---|
0 | 1.0 | Setup | import pandas as pd\nimport numpy as np | using DataFrames\nusing Statistics\nusing CSV |
1 | 1.0 | Setup | pip install pandas | using Pkg\nPkg.add(\"JSON\") |
2 | 1.0 | Setup | https://pypi.org/ | https://juliapackages.com/ |
3 | 2.0 | Data Structures | s = pd.Series(['a', 'b', 'c'], index=[0 , 1, 2]) | s = [1, 2, 3] |
4 | 2.0 | Data Structures | s[0] | s[1] |