I would like to generate several notebooks from a python script. Is there an API to write IPython notebooks?
There is, you can do:
import io
from IPython.nbformat import current
def convert(py_file, ipynb_file):
    with io.open(py_file, 'r', encoding='utf-8') as f:
        notebook = current.reads(f.read(), format='py')
    with io.open(ipynb_file, 'w', encoding='utf-8') as f:
        current.write(notebook, f, format='ipynb')
convert('test.py', 'test.ipynb')
But it's not that smart and it will place all the code from the python file into one IPython Notebook cell. But you can always do a little bit of parsing.
import io
import re
from IPython.nbformat import current
def parse_into_cells(py_file):
    with io.open(py_file, 'r', encoding='utf-8') as f:
        data = f.readlines()
    in_cell = True
    cell = ''
    for line in data:
        if line.rstrip() == '':
            # If a blank line occurs I'm out of the current cell
            in_cell = False
        elif re.match('^\s+', line):
            # Indentation, so nope, I'm not out of the current cell
            in_cell = True
            cell += line
        else:
            # Code at the beginning of the line, so if I'm in a cell just
            # append it, otherwise yield out the cell and start a new one
            if in_cell:
                cell += line
            else:
                yield cell.strip()
                cell = line
                in_cell = True
    if cell != '':
        yield cell.strip()
def convert(py_file, ipynb_file):
    # Create an empty notebook
    notebook = current.reads('', format='py')
    # Add all the parsed cells
    notebook['worksheets'][0]['cells'] = list(map(current.new_code_cell,
                                                  parse_into_cells(py_file)))
    # Save the notebook
    with io.open(ipynb_file, 'w', encoding='utf-8') as f:
        current.write(notebook, f, format='ipynb')
convert('convert.py', 'convert.ipynb')
Edit: Explaining the parsing
In the previous code a cell split is triggered whenever a blank line appears before a module level instruction (function, variable or class definition, import, etc.). That is whenever I see a line that is not indented and is preceded with a blank line). So:
import time
import datetime
Will be just one cell, but:
import time
import datetime
Will be two cells, and also
class Test(objet):
    def __init__(self, x):
        self.x = x
    def show(self):
        print(self.x)
class Foo(object):
     pass
will be two cells since there are only two top level definitions (lines that are not indented) that are preceded with a blank line (first line in the file is considered to be preceded with a blank line because it has to start a new cell).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With