-
-
Save vyraun/62381cd89f811131a121fff9c0e0e1b6 to your computer and use it in GitHub Desktop.
Creating an IPython Notebook programatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Creating an IPython Notebook programatically\n", | |
"\nThe `nbformat` package gives us the necessary tools to create a new Jupyter Notebook without having to know the specifics of the file format, JSON schema, etc." | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import nbformat as nbf" | |
], | |
"outputs": [], | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Now we create a new notebook object, that we can then populate with cells, metadata, etc:" | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"nb = nbf.v4.new_notebook()" | |
], | |
"outputs": [], | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Our simple text notebook will only have a text cell and a code cell:" | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"text = \"\"\"\\\n", | |
"# My first automatic Jupyter Notebook\n", | |
"This is an auto-generated notebook.\"\"\"\n", | |
"\n", | |
"code = \"\"\"\\\n", | |
"%pylab inline\n", | |
"hist(normal(size=2000), bins=50);\"\"\"\n", | |
"\n", | |
"nb['cells'] = [nbf.v4.new_markdown_cell(text),\n", | |
" nbf.v4.new_code_cell(code) ]" | |
], | |
"outputs": [], | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Next, we write it to a file on disk that we can then open as a new notebook.\n", | |
"\n", | |
"Note: This should be as easy as: `nbf.write(nb, fname)`, but the current api is a little more verbose and needs a real file-like\n", | |
"object." | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"fname = 'test.ipynb'\n", | |
"\n", | |
"with open(fname, 'w') as f:\n", | |
" nbf.write(nb, f)" | |
], | |
"outputs": [], | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"This notebook can be run at the command line with:\n", | |
"\n", | |
" jupyter nbconvert --execute --inplace test.ipynb\n", | |
"\nOr you can open it [as a live notebook](test.ipynb)." | |
], | |
"metadata": {} | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"name": "python3", | |
"language": "python", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.0", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"kernel_info": { | |
"name": "python3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment