如何通过代码(Python)创建/修改Jupyter Notebook? [英] How to create/modify a jupyter notebook from code (python)?

查看:127
本文介绍了如何通过代码(Python)创建/修改Jupyter Notebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的项目创建过程自动化,并希望作为其一部分来创建一个新的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 Notebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆