Last active
November 14, 2024 11:22
-
-
Save fperez/9716279 to your computer and use it in GitHub Desktop.
Creating an IPython Notebook programatically
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\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 | |
} |
Very good job ! Tanks
Thank you very much
Thanks
Worked on the very first try. That easy. Thank you so much.
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?
Awesome thanks!
Specify complete path test.ipynb
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fernando, thanks a lot for this example. We will try to get it running for Dataverse.