Issue
I want to edit (add code cells) and run a jupyter notebook file (notebook.ipynb), all within a python script (main.py). While I am able to run the notebook using nbconvert, I'm looking for a way to add a few code cells to notebook.ipynb before executing it. What would be a good way to achieve this?
Solution
There is a nbformat
library that you can use for reading/writing notebooks. It has function new_code_cell()
that you can use to add new cells to the notebook.
The nbconvert
package has Python API for executing notebooks (ExecuteProcessor
docs).
Some simple example:
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
# read notebook
with open("my-amazing-notebook.ipynb") as f:
nb = nbformat.read(f, as_version=4)
# add code
nb["cells"] += [nbformat.v4.new_code_cell("a = 13")]
# execute notebook
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(nb, {'metadata': {'path': 'notebooks/'}})
# write notebook
with open('executed-notebook.ipynb', 'w', encoding='utf-8') as f:
nbformat.write(nb, f)
Answered By - pplonski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.