How to create/modify a jupyter notebook from code (python)?

后端 未结 2 1889
既然无缘
既然无缘 2021-02-07 13:01

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 i

2条回答
  •  被撕碎了的回忆
    2021-02-07 13:12

    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)
    

提交回复
热议问题