5

我想从 python 脚本生成几个笔记本。是否有编写 IPython 笔记本的 API?

4

1 回答 1

4

有,你可以这样做:

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')

但它并不那么聪明,它会将 python 文件中的所有代码放入一个 IPython Notebook 单元中。但是你总是可以做一点解析。

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')

编辑:解释解析

在前面的代码中,只要在模块级指令(函数、变量或类定义、导入等)之前出现空行,就会触发单元拆分。那就是每当我看到一行没有缩进并且前面有一个空行时)。所以:

import time
import datetime

将只是一个单元格,但是:

import time

import datetime

将是两个单元格,并且还

class Test(objet):

    def __init__(self, x):

        self.x = x

    def show(self):

        print(self.x)

class Foo(object):
     pass

将是两个单元格,因为只有两个顶级定义(未缩进的行)前面有一个空行(文件中的第一行被认为前面有一个空行,因为它必须开始一个新的单元格) .

于 2013-08-01T18:14:18.817 回答