Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@4tikhonov

This comment has been minimized.

Copy link

@4tikhonov 4tikhonov commented Oct 13, 2016

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

@joao-parana

This comment has been minimized.

Copy link

@joao-parana joao-parana commented Apr 21, 2017

Very good job ! Tanks

@Oliph

This comment has been minimized.

Copy link

@Oliph Oliph commented Jun 30, 2017

Thank you very much

@zbenmo

This comment has been minimized.

Copy link

@zbenmo zbenmo commented Oct 29, 2018

Thanks

@roodee

This comment has been minimized.

Copy link

@roodee roodee commented Jan 28, 2019

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

@juanp1245

This comment has been minimized.

Copy link

@juanp1245 juanp1245 commented Sep 18, 2019

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

This comment has been minimized.

Copy link

@lambdamusic lambdamusic commented Mar 8, 2020

Awesome thanks!

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