Skip to content

Instantly share code, notes, and snippets.

@fperez
Last active May 2, 2024 19:14
Show Gist options
  • Save fperez/9716279 to your computer and use it in GitHub Desktop.
Save fperez/9716279 to your computer and use it in GitHub Desktop.
Creating an IPython Notebook programatically
Display the source blob
Display the rendered blob
Raw
{
"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
}
@4tikhonov
Copy link

Fernando, thanks a lot for this example. We will try to get it running for Dataverse.

@joao-parana
Copy link

Very good job ! Tanks

@Oliph
Copy link

Oliph commented Jun 30, 2017

Thank you very much

@zbenmo
Copy link

zbenmo commented Oct 29, 2018

Thanks

@roodee
Copy link

roodee commented Jan 28, 2019

Worked on the very first try. That easy. Thank you so much.

@juanp1245
Copy link

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?

@lambdamusic
Copy link

Awesome thanks!

@saraswati02
Copy link

Specify complete path test.ipynb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment