加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

是否有IPython笔记本API?

发布时间:2020-12-16 21:49:05 所属栏目:Python 来源:网络整理
导读:我想从python脚本生成几个笔记本.是否有编写IPython笔记本的API? 最佳答案 有,你可以这样做: import iofrom IPython.nbformat import currentdef convert(py_file,ipynb_file): with io.open(py_file,'r',encoding='utf-8') as f: notebook = current.read

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

最佳答案
有,你可以这样做:

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,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,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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读