genipynb

script to turn py files into ipynb files.

used to turn rst files i write into py files, then into ipynb.

Open up all ipynbs - Filter all markdown text into one notebook.


In [ ]:


In [1]:
import os
from base64 import encodestring

from IPython.nbformat.v4.nbbase import (
    new_code_cell, new_markdown_cell, new_notebook,
    new_output, new_raw_cell
)

import IPython.nbformat as nbf
import codecs


/usr/local/lib/python3.4/dist-packages/IPython/nbformat.py:13: ShimWarning: The `IPython.nbformat` package has been deprecated. You should import from nbformat instead.
  "You should import from nbformat instead.", ShimWarning)

In [6]:
oprsf = open('/home/wcmckee/github/bb.com/posts/js13k.rst','r')

In [7]:
opr = oprsf.read()

In [ ]:


In [9]:
# -*- coding: utf-8 -*-

# some random base64-encoded *text*
png = encodestring(os.urandom(5)).decode('ascii')
jpeg = encodestring(os.urandom(6)).decode('ascii')

cells = []
cells.append(new_markdown_cell(
    source= opr))


cells.append(new_code_cell(
    source='import numpy',
    execution_count=1,
))

cells.append(new_markdown_cell(
    source='A random array',
))


cells.append(new_markdown_cell(
    source=u'## My Heading',
))

cells.append(new_code_cell(
    source='a = numpy.random.rand(100)',
    execution_count=2,
))
cells.append(new_code_cell(
    source='a = 10\nb = 5\n',
    execution_count=3,
))
cells.append(new_code_cell(
    source='a = 10\nb = 5',
    execution_count=4,
))

cells.append(new_code_cell(
    source=u'print "ünîcødé"',
    execution_count=3,
    outputs=[new_output(
        output_type=u'execute_result',
        data={
            'text/plain': u'<array a>',
            'text/html': u'The HTML rep',
            'text/latex': u'$a$',
            'image/png': png,
            'image/jpeg': jpeg,
            'image/svg+xml': u'<svg>',
            'application/json': {
                'key': 'value'
            },
            'application/javascript': u'var i=0;'
        },
        execution_count=3
    ),new_output(
        output_type=u'display_data',
        data={
            'text/plain': u'<array a>',
            'text/html': u'The HTML rep',
            'text/latex': u'$a$',
            'image/png': png,
            'image/jpeg': jpeg,
            'image/svg+xml': u'<svg>',
            'application/json': {
                'key': 'value'
            },
            'application/javascript': u'var i=0;'
        },
    ),new_output(
        output_type=u'error',
        ename=u'NameError',
        evalue=u'NameError was here',
        traceback=[u'frame 0', u'frame 1', u'frame 2']
    ),new_output(
        output_type=u'stream',
        text='foo\rbar\r\n'
    ),new_output(
        output_type=u'stream',
        name='stderr',
        text='\rfoo\rbar\n'
    )]
))

nb0 = new_notebook(cells=cells,
    metadata={
        'language': 'python',
    }
)


f = codecs.open('test.ipynb', encoding='utf-8', mode='w')
nbf.write(nb0, f, 4)
f.close()


/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py:4: DeprecationWarning: encodestring() is a deprecated alias, use encodebytes()
/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py:5: DeprecationWarning: encodestring() is a deprecated alias, use encodebytes()

In [ ]:


In [5]:
cat test.ipynb


{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some NumPy Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A random array"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "A random array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## My Heading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = numpy.random.rand(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "b = 5\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "b = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "var i=0;"
      ],
      "application/json": {
       "key": "value"
      },
      "image/jpeg": "KnmibAhG\n",
      "image/png": "DcFiDwM=\n",
      "image/svg+xml": [
       "<svg>"
      ],
      "text/html": [
       "The HTML rep"
      ],
      "text/latex": [
       "$a$"
      ],
      "text/plain": [
       "<array a>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/javascript": [
       "var i=0;"
      ],
      "application/json": {
       "key": "value"
      },
      "image/jpeg": "KnmibAhG\n",
      "image/png": "DcFiDwM=\n",
      "image/svg+xml": [
       "<svg>"
      ],
      "text/html": [
       "The HTML rep"
      ],
      "text/latex": [
       "$a$"
      ],
      "text/plain": [
       "<array a>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "NameError",
     "evalue": "NameError was here",
     "output_type": "error",
     "traceback": [
      "frame 0",
      "frame 1",
      "frame 2"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo\r",
      "bar\r\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "foo\r",
      "bar\n"
     ]
    }
   ],
   "source": [
    "print \"ünîcødé\""
   ]
  }
 ],
 "metadata": {
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

In [ ]: