Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lumixraku/913ef8dec599168e2e4047861baeaab8 to your computer and use it in GitHub Desktop.

Select an option

Save lumixraku/913ef8dec599168e2e4047861baeaab8 to your computer and use it in GitHub Desktop.

Revisions

  1. @fperez fperez revised this gist Mar 31, 2018. 1 changed file with 119 additions and 125 deletions.
    244 changes: 119 additions & 125 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,127 +1,121 @@
    {
    "cells": [
    {
    "cell_type": "markdown",
    "source": [
    "# Creating an IPython Notebook programatically\n",
    "\nThe `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."
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "import nbformat as nbf"
    ],
    "outputs": [],
    "execution_count": 1,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "source": [
    "Now we create a new notebook object, that we can then populate with cells, metadata, etc:"
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "nb = nbf.v4.new_notebook()"
    ],
    "outputs": [],
    "execution_count": 2,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "source": [
    "Our simple text notebook will only have a text cell and a code cell:"
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "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) ]"
    ],
    "outputs": [],
    "execution_count": 10,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "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."
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "fname = 'test.ipynb'\n",
    "\n",
    "with open(fname, 'w') as f:\n",
    " nbf.write(nb, f)"
    ],
    "outputs": [],
    "execution_count": 11,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "source": [
    "This notebook can be run at the command line with:\n",
    "\n",
    " jupyter nbconvert --execute --inplace test.ipynb\n",
    "\nOr you can open it [as a live notebook](test.ipynb)."
    ],
    "metadata": {}
    }
    ],
    "metadata": {
    "anaconda-cloud": {},
    "kernelspec": {
    "name": "python3",
    "language": "python",
    "display_name": "Python 3"
    },
    "language_info": {
    "name": "python",
    "version": "3.6.0",
    "mimetype": "text/x-python",
    "codemirror_mode": {
    "name": "ipython",
    "version": 3
    },
    "pygments_lexer": "ipython3",
    "nbconvert_exporter": "python",
    "file_extension": ".py"
    },
    "kernel_info": {
    "name": "python3"
    }
    "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."
    ]
    },
    "nbformat": 4,
    "nbformat_minor": 1
    }
    {
    "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
    }
  2. @fperez fperez revised this gist Feb 23, 2017. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -47,16 +47,19 @@
    {
    "cell_type": "code",
    "source": [
    "text = \"This is an auto-generated notebook.\"\n",
    "code = \"\"\"\n",
    "text = \"\"\"\\\n",
    "# My first automatic Jupyter Notebook\n",
    "This is an auto-generated notebook.\"\"\"\n",
    "\n",
    "code = \"\"\"\\\n",
    "%pylab inline\n",
    "plot(rand(100))\"\"\"\n",
    "hist(normal(size=2000), bins=50);\"\"\"\n",
    "\n",
    "nb['cells'] = [nbf.v4.new_markdown_cell(text),\n",
    " nbf.v4.new_code_cell(code) ]"
    ],
    "outputs": [],
    "execution_count": 3,
    "execution_count": 10,
    "metadata": {
    "collapsed": false
    }
    @@ -80,7 +83,7 @@
    " nbf.write(nb, f)"
    ],
    "outputs": [],
    "execution_count": 4,
    "execution_count": 11,
    "metadata": {
    "collapsed": false
    }
  3. @fperez fperez revised this gist Feb 23, 2017. 1 changed file with 121 additions and 112 deletions.
    233 changes: 121 additions & 112 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,115 +1,124 @@
    {
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Creating an IPython Notebook programatically"
    ]
    "cells": [
    {
    "cell_type": "markdown",
    "source": [
    "# Creating an IPython Notebook programatically\n",
    "\nThe `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."
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "import nbformat as nbf"
    ],
    "outputs": [],
    "execution_count": 1,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "source": [
    "Now we create a new notebook object, that we can then populate with cells, metadata, etc:"
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "nb = nbf.v4.new_notebook()"
    ],
    "outputs": [],
    "execution_count": 2,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "source": [
    "Our simple text notebook will only have a text cell and a code cell:"
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "text = \"This is an auto-generated notebook.\"\n",
    "code = \"\"\"\n",
    "%pylab inline\n",
    "plot(rand(100))\"\"\"\n",
    "\n",
    "nb['cells'] = [nbf.v4.new_markdown_cell(text),\n",
    " nbf.v4.new_code_cell(code) ]"
    ],
    "outputs": [],
    "execution_count": 3,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "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."
    ],
    "metadata": {}
    },
    {
    "cell_type": "code",
    "source": [
    "fname = 'test.ipynb'\n",
    "\n",
    "with open(fname, 'w') as f:\n",
    " nbf.write(nb, f)"
    ],
    "outputs": [],
    "execution_count": 4,
    "metadata": {
    "collapsed": false
    }
    },
    {
    "cell_type": "markdown",
    "source": [
    "This notebook can be run at the command line with:\n",
    "\n",
    " jupyter nbconvert --execute --inplace test.ipynb\n",
    "\nOr you can open it [as a live notebook](test.ipynb)."
    ],
    "metadata": {}
    }
    ],
    "metadata": {
    "anaconda-cloud": {},
    "kernelspec": {
    "name": "python3",
    "language": "python",
    "display_name": "Python 3"
    },
    "language_info": {
    "name": "python",
    "version": "3.6.0",
    "mimetype": "text/x-python",
    "codemirror_mode": {
    "name": "ipython",
    "version": 3
    },
    "pygments_lexer": "ipython3",
    "nbconvert_exporter": "python",
    "file_extension": ".py"
    },
    "kernel_info": {
    "name": "python3"
    }
    },
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "import nbformat as nbf"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "nb = nbf.v4.new_notebook()"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "This notebook will simply have three cells that read `print 0`, `print 1`, etc:"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "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."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "fname = 'test.ipynb'\n",
    "\n",
    "with open(fname, '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 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.5.1"
    },
    "widgets": {
    "state": {},
    "version": "1.1.1"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 1
    "nbformat": 4,
    "nbformat_minor": 1
    }
  4. @fperez fperez revised this gist Jun 23, 2016. 1 changed file with 111 additions and 116 deletions.
    227 changes: 111 additions & 116 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,120 +1,115 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:de19c92dacc550f16a6a28a06419f10830ed780a132b6b565758a6a675565fe5"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    "worksheets": [
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Creating an IPython Notebook programatically"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "import nbformat as nbf"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "nb = nbf.v4.new_notebook()"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "This notebook will simply have three cells that read `print 0`, `print 1`, etc:"
    ]
    },
    {
    "cells": [
    {
    "cell_type": "heading",
    "level": 1,
    "metadata": {},
    "source": [
    "Creating an IPython Notebook programatically"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "from IPython.nbformat import current as nbf"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 3
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "nb = nbf.new_notebook()"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 13
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "This notebook will simply have three cells that read `print 0`, `print 1`, etc:"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "text = \"This is an auto-generated notebook.\"\n",
    "code = \"1+2\"\n",
    "\n",
    "cells = [nbf.new_text_cell('markdown', text),\n",
    " nbf.new_code_cell(code) ]"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 14
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Now that we have the cells, we can make a worksheet with them and add it to the notebook:"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "nb['worksheets'].append(nbf.new_worksheet(cells=cells))"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 15
    },
    {
    "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."
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "fname = 'test.ipynb'\n",
    "\n",
    "with open(fname, 'w') as f:\n",
    " nbf.write(nb, f, 'ipynb')"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 16
    },
    {
    "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": {}
    "cell_type": "code",
    "execution_count": 3,
    "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."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "fname = 'test.ipynb'\n",
    "\n",
    "with open(fname, '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 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.5.1"
    },
    "widgets": {
    "state": {},
    "version": "1.1.1"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 1
    }
  5. @fperez fperez revised this gist Dec 5, 2014. 1 changed file with 1 addition and 12 deletions.
    13 changes: 1 addition & 12 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:ed5456a1541023cdf27c5b8eb72bab829eda670dea24ecc315386aa52691c7a9"
    "signature": "sha256:de19c92dacc550f16a6a28a06419f10830ed780a132b6b565758a6a675565fe5"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    @@ -38,17 +38,6 @@
    "outputs": [],
    "prompt_number": 13
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "nbf.new_text_cell??"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 9
    },
    {
    "cell_type": "markdown",
    "metadata": {},
  6. @fperez fperez revised this gist Dec 5, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:3e284ab47065d8a0c57ee23bf8fc1c293e78f7a42f6e7d8bab1a8097d5ae9d59"
    "signature": "sha256:ed5456a1541023cdf27c5b8eb72bab829eda670dea24ecc315386aa52691c7a9"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    @@ -64,8 +64,7 @@
    "code = \"1+2\"\n",
    "\n",
    "cells = [nbf.new_text_cell('markdown', text),\n",
    " nbf.new_code_cell(code) ]\n",
    "#cells = [ nbf.new_code_cell(\"print %i\" % i ) for i in range(3) ]"
    " nbf.new_code_cell(code) ]"
    ],
    "language": "python",
    "metadata": {},
  7. @fperez fperez revised this gist Dec 5, 2014. 1 changed file with 23 additions and 7 deletions.
    30 changes: 23 additions & 7 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:fc03806fdeedea39785f76d073774a126271ab80c13f6b3cdf5c432431fb327e"
    "signature": "sha256:3e284ab47065d8a0c57ee23bf8fc1c293e78f7a42f6e7d8bab1a8097d5ae9d59"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    @@ -25,7 +25,7 @@
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 1
    "prompt_number": 3
    },
    {
    "cell_type": "code",
    @@ -36,7 +36,18 @@
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 2
    "prompt_number": 13
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "nbf.new_text_cell??"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 9
    },
    {
    "cell_type": "markdown",
    @@ -49,12 +60,17 @@
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "cells = [ nbf.new_code_cell(\"print %i\" % i ) for i in range(3) ]"
    "text = \"This is an auto-generated notebook.\"\n",
    "code = \"1+2\"\n",
    "\n",
    "cells = [nbf.new_text_cell('markdown', text),\n",
    " nbf.new_code_cell(code) ]\n",
    "#cells = [ nbf.new_code_cell(\"print %i\" % i ) for i in range(3) ]"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 3
    "prompt_number": 14
    },
    {
    "cell_type": "markdown",
    @@ -72,7 +88,7 @@
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 4
    "prompt_number": 15
    },
    {
    "cell_type": "markdown",
    @@ -96,7 +112,7 @@
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 5
    "prompt_number": 16
    },
    {
    "cell_type": "markdown",
  8. @fperez fperez revised this gist Mar 23, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:111336fd90e40b178363fb7aec49e144c2d592316ecf2365580e8cc7e51b8887"
    "signature": "sha256:fc03806fdeedea39785f76d073774a126271ab80c13f6b3cdf5c432431fb327e"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    @@ -42,7 +42,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "This notebook will simply have three cells that read `print 1`, `print 2`, etc:"
    "This notebook will simply have three cells that read `print 0`, `print 1`, etc:"
    ]
    },
    {
  9. @fperez fperez revised this gist Mar 23, 2014. 1 changed file with 6 additions and 43 deletions.
    49 changes: 6 additions & 43 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:091e85f0b0a9b6046ac3ff3a934f44b0ab25793466640e783e466da776eda240"
    "signature": "sha256:111336fd90e40b178363fb7aec49e144c2d592316ecf2365580e8cc7e51b8887"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    @@ -102,49 +102,12 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "For convenience, link and execution instructions:"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "from IPython.display import FileLink\n",
    "This notebook can be run at the command line with:\n",
    "\n",
    "print \"This notebook can be run at the command line with:\"\n",
    "print\n",
    "print \"ipython -c '%%run %s'\" % fname\n",
    "print \n",
    "print \"Or you can open it as a notebook here:\"\n",
    "FileLink(fname)"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [
    {
    "output_type": "stream",
    "stream": "stdout",
    "text": [
    "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 notebook here:\n"
    ]
    },
    {
    "html": [
    "<a href='test.ipynb' target='_blank'>test.ipynb</a><br>"
    ],
    "metadata": {},
    "output_type": "pyout",
    "prompt_number": 6,
    "text": [
    "/home/fperez/test.ipynb"
    ]
    }
    ],
    "prompt_number": 6
    " ipython -c '%run test.ipynb'\n",
    "\n",
    "Or you can open it [as a live notebook](test.ipynb)."
    ]
    }
    ],
    "metadata": {}
  10. @fperez fperez created this gist Mar 22, 2014.
    153 changes: 153 additions & 0 deletions ProgrammaticNotebook.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,153 @@
    {
    "metadata": {
    "name": "",
    "signature": "sha256:091e85f0b0a9b6046ac3ff3a934f44b0ab25793466640e783e466da776eda240"
    },
    "nbformat": 3,
    "nbformat_minor": 0,
    "worksheets": [
    {
    "cells": [
    {
    "cell_type": "heading",
    "level": 1,
    "metadata": {},
    "source": [
    "Creating an IPython Notebook programatically"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "from IPython.nbformat import current as nbf"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 1
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "nb = nbf.new_notebook()"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 2
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "This notebook will simply have three cells that read `print 1`, `print 2`, etc:"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "cells = [ nbf.new_code_cell(\"print %i\" % i ) for i in range(3) ]"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 3
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Now that we have the cells, we can make a worksheet with them and add it to the notebook:"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "nb['worksheets'].append(nbf.new_worksheet(cells=cells))"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 4
    },
    {
    "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."
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "fname = 'test.ipynb'\n",
    "\n",
    "with open(fname, 'w') as f:\n",
    " nbf.write(nb, f, 'ipynb')"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [],
    "prompt_number": 5
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "For convenience, link and execution instructions:"
    ]
    },
    {
    "cell_type": "code",
    "collapsed": false,
    "input": [
    "from IPython.display import FileLink\n",
    "\n",
    "print \"This notebook can be run at the command line with:\"\n",
    "print\n",
    "print \"ipython -c '%%run %s'\" % fname\n",
    "print \n",
    "print \"Or you can open it as a notebook here:\"\n",
    "FileLink(fname)"
    ],
    "language": "python",
    "metadata": {},
    "outputs": [
    {
    "output_type": "stream",
    "stream": "stdout",
    "text": [
    "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 notebook here:\n"
    ]
    },
    {
    "html": [
    "<a href='test.ipynb' target='_blank'>test.ipynb</a><br>"
    ],
    "metadata": {},
    "output_type": "pyout",
    "prompt_number": 6,
    "text": [
    "/home/fperez/test.ipynb"
    ]
    }
    ],
    "prompt_number": 6
    }
    ],
    "metadata": {}
    }
    ]
    }