可以使用 nbformat 来动态创建'.ipynb'文件,然后用jupyter nbconvert
执行
import nbformat as nbf
nb = nbf.v4.new_notebook()
text = "This is an auto-generated notebook."
code = "1+2"
nb['cells'] = [nbf.v4.new_markdown_cell(text),
nbf.v4.new_code_cell(code) ]
fname = 'x.ipynb'
with open(fname, 'w') as f:
nbf.write(nb, f)
创建好的'.ipynb'文件里的cell是未执行的。
# 导出html文件
jupyter nbconvert --execute --to html x.ipynb
# 转化为新的执行后的'.ipynb'文件
jupyter nbconvert --execute --to notebook x.ipynb
参考: