Jupyter (IPython) notebook:将 HTML notebook 转换为 ipynb [英] Jupyter (IPython) notebook: Convert an HTML notebook to ipynb
问题描述
我已将 Jupyter/IPython 笔记本转换为 HTML 格式,随后丢失了原始 ipynb 文件.
有没有一种简单的方法可以从转换后的 HTML 文件中生成原始笔记本文件?
我最近使用 BeautifulSoup 和 JSON 将 html notebook 转换为 ipynb.诀窍是查看笔记本的 JSON 模式并模拟它.代码只选择输入代码单元格和降价单元格
这是我的代码
from bs4 import BeautifulSoup导入json导入 urllib.requesturl = 'http://nbviewer.jupyter.org/url/jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb'响应 = urllib.request.urlopen(url)# 本地html文件# response = open("/Users/note/jupyter/notebook.html")文本 = response.read()汤 = BeautifulSoup(文本,'lxml')# 看一些html打印(汤.div)字典 = {'nbformat': 4, 'nbformat_minor': 1, 'cells': [], 'metadata': {}}对于 d 汤.findAll("div"):如果 d.attrs.keys() 中的类":对于 d.attrs["class"] 中的类:如果 ["text_cell_render", "input_area"] 中的类:# 代码单元格如果类 == "input_area":单元格 = {}单元格['元数据'] = {}单元格['输出'] = []单元格['源'] = [d.get_text()]单元格 ['execution_count'] = 无单元格['cell_type'] = '代码'字典['cells'].append(cell)别的:单元格 = {}单元格['元数据'] = {}单元格['源'] = [d.decode_contents()]单元格['cell_type'] = '降价'字典['cells'].append(cell)open('notebook.ipynb', 'w').write(json.dumps(dictionary))
这是print(soup.div)
输出的一部分
div class="container"><div class="navbar-header"><button class="navbar-toggle collapsed" data-target=".navbar-collapse" data-toggle="collapse" type="button"><span class="sr-only">切换导航</span><i class="fa fa-bars"></i>按钮><a class="navbar-brand" href="/"><img src="/static/img/nav_logo.svg?v=479cefe8d932fb14a67b93911b97d70f" width="159"/></a>
<div class="collapse navbar-collapse"><ul class="nav navbar-nav navbar-right"><li><a class="active" href="http://jupyter.org">JUPYTER</a><li>
I have converted a Jupyter/IPython notebook to HTML format and subsequently lost the original ipynb file.
Is there a simple way to generate the original notebook file from the converted HTML file?
I recently used BeautifulSoup and JSON to convert html notebook to ipynb. the trick is to look at the JSON schema of a notebook and emulate that. The code selects only input code cells and markdown cells
here is my code
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))
here is part of print(soup.div)
output
div class="container">
<div class="navbar-header">
<button class="navbar-toggle collapsed" data-target=".navbar-collapse" data-toggle="collapse" type="button">
<span class="sr-only">Toggle navigation</span>
<i class="fa fa-bars"></i>
</button>
<a class="navbar-brand" href="/">
<img src="/static/img/nav_logo.svg?v=479cefe8d932fb14a67b93911b97d70f" width="159"/>
</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<a class="active" href="http://jupyter.org">JUPYTER</a>
</li>
<li>
<a href="/faq" title="FAQ">
<span>FAQ</span>
A screen shot of the resulting ipynb file, loaded on my local jupyter and after running all the cells
这篇关于Jupyter (IPython) notebook:将 HTML notebook 转换为 ipynb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!