将HTML发布的Jupyter Notebook转换为可执行的Jupyter.ipynb文件的文件

问题描述 投票:0回答:1

我有Jupyter Notebook的HTML发布版本,我需要将它们批量转换回可执行的Jupyter.ipynb文件。我发现了很多讨论和方法,涉及如何从Jupyter.ipynb文件发布到HTML文件。每个Jupyter NB Web Client的“文件...”菜单下均包含一项功能,该功能可以将HTML发布为HTML或将HTML作为许多选项之一的“下载为...”。但是,没有“导入Jupyter”或“从HTML导入”功能。在这种情况下我会丢失什么吗?这并不是必需的。

编写我自己的网络爬虫以刮擦Jupyter NB的HTML发布版本,然后以编程方式创建IPython NB文件格式的JSON NB结构,有没有更简单的方法?

我已经尝试了IPython notebook: Convert an HTML notebook to ipynb中的以下代码,并获得了不错的结果,但这仅捕获并转换代码单元和减价单元。

from bs4 import BeautifulSoup
import json
import urllib.request
url = 'http://nbviewer.jupyter.org/url/jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb'
response = urllib.request.urlopen(url)
#  for local html file
# response = open("/Users/note/jupyter/notebook.html")
text = response.read()

soup = BeautifulSoup(text, 'lxml')
# see some of the html
print(soup.div)
dictionary = {'nbformat': 4, 'nbformat_minor': 1, 'cells': [], 'metadata': {}}
for d in soup.findAll("div"):
    if 'class' in d.attrs.keys():
        for clas in d.attrs["class"]:
            if clas in ["text_cell_render", "input_area"]:
                # code cell
                if clas == "input_area":
                    cell = {}
                    cell['metadata'] = {}
                    cell['outputs'] = []
                    cell['source'] = [d.get_text()]
                    cell['execution_count'] = None
                    cell['cell_type'] = 'code'
                    dictionary['cells'].append(cell)

                else:
                    cell = {}
                    cell['metadata'] = {}

                    cell['source'] = [d.decode_contents()]
                    cell['cell_type'] = 'markdown'
                    dictionary['cells'].append(cell)
open('notebook.ipynb', 'w').write(json.dumps(dictionary))

它不会转换整个笔记本,也不会以批处理模式进行。

html jupyter-notebook jupyter file-conversion
1个回答
0
投票

Pandoc现在从笔记本的HTML版本转换回笔记本。参见here

© www.soinside.com 2019 - 2024. All rights reserved.