Last active
August 29, 2015 14:10
-
-
Save fperez/b3ab79a32b0e4777a646 to your computer and use it in GitHub Desktop.
Bug in programmatic notebook creation?
This file contains 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", | |
"metadata": {}, | |
"source": [ | |
"# Creating an IPython Notebook programatically" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import IPython.nbformat as nbf" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's create the new notebook object first:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"nb = nbf.v4.new_notebook()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"For this example, we populate it with a simple text cell and one with code. Empty notebooks are OK too, this is just for illustration purposes:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"text = \"This is an auto-generated notebook.\"\n", | |
"code = \"1+2\"\n", | |
"\n", | |
"nb['cells'] = [nbf.v4.new_markdown_cell(text),\n", | |
" nbf.v4.new_code_cell(code) ]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"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. We're fixing that." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"filename = 'test.ipynb'\n", | |
"\n", | |
"with open(filename, 'w') as f:\n", | |
" nbf.write(nb, f)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This notebook can be run at the command line with:\n", | |
"\n", | |
" ipython -c '%run test.ipynb'\n", | |
"\n", | |
"Or you can open it [as a live notebook](test.ipynb)." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "IPython (Python 2)", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.5+" | |
}, | |
"signature": "sha256:daf65ba716d91340958662ecb313dece45cefb4286ed0aeb9e7f8c73ffd4b595" | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment