Skip to content

Instantly share code, notes, and snippets.

@notsobad
Last active August 10, 2016 03:51
Show Gist options
  • Save notsobad/ebe3a4b1676a7147a6bd39824ca5f1e8 to your computer and use it in GitHub Desktop.
Save notsobad/ebe3a4b1676a7147a6bd39824ca5f1e8 to your computer and use it in GitHub Desktop.

可以使用 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

参考:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment