如何从代码(python)创建/修改 jupyter 笔记本? [英] How to create/modify a jupyter notebook from code (python)?
本文介绍了如何从代码(python)创建/修改 jupyter 笔记本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试自动化我的项目创建过程,并希望作为其中的一部分创建一个新的 jupyter 笔记本,并在其中填充我通常在每个笔记本中都有的一些单元格和内容(即导入、标题等)
I am trying to automate my project create process and would like as part of it to create a new jupyter notebook and populate it with some cells and content that I usually have in every notebook (i.e., imports, titles, etc.)
是否可以通过python来做到这一点?
Is it possible to do this via python?
推荐答案
您可以使用 nbformat 来完成.下面是以编程方式创建 IPython Notebook 的示例:
You can do it using nbformat. Below an example taken from Creating an IPython Notebook programatically:
import nbformat as nbf
nb = nbf.v4.new_notebook()
text = """
# My first automatic Jupyter Notebook
This is an auto-generated notebook."""
code = """
%pylab inline
hist(normal(size=2000), bins=50);"""
nb['cells'] = [nbf.v4.new_markdown_cell(text),
nbf.v4.new_code_cell(code)]
fname = 'test.ipynb'
with open(fname, 'w') as f:
nbf.write(nb, f)
这篇关于如何从代码(python)创建/修改 jupyter 笔记本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文