Creating an IPython Notebook programatically
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Creating an IPython Notebook programatically\n", | |
"\n", | |
"The `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." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import nbformat as nbf" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now we create a new notebook object, that we can then populate with cells, metadata, etc:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"nb = nbf.v4.new_notebook()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Our simple text notebook will only have a text cell and a code cell:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"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) ]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Next, we write it to a file on disk that we can then open as a new notebook:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nbf.write(nb, 'test.ipynb')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This notebook can be run at the command line with:\n", | |
"\n", | |
" jupyter nbconvert --execute --inplace test.ipynb\n", | |
"\n", | |
"Or you can open it [as a live notebook](test.ipynb)." | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernel_info": { | |
"name": "python3" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
This comment has been minimized.
This comment has been minimized.
Very good job ! Tanks |
This comment has been minimized.
This comment has been minimized.
Thank you very much |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
Worked on the very first try. That easy. Thank you so much. |
This comment has been minimized.
This comment has been minimized.
It worked, thank you! But i have a question, how can i specify where in my folders to save the .ipynb file? where do i write the route? |
This comment has been minimized.
This comment has been minimized.
Awesome thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Fernando, thanks a lot for this example. We will try to get it running for Dataverse.