我知道我可以打电话
jupyter nbconvert --execute --to html notebook.ipynb从shell执行系统调用,或者从Python执行系统调用:
import os
os.system('jupyter nbconvert --execute --to html notebook.ipynb')但是,当nbconvert模块在本地Python中可用时,必须使用系统调用来完成这一任务似乎非常奇怪!
我想编写原生python来执行iPython笔记本并将其转换为html文件。我稍微研究了一下正式文件,但不能把东西拼凑在一起。我的问题是:
发布于 2020-12-30 05:33:44
好吧,经过一些尝试和错误之后,我发现:
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert import HTMLExporter
# read source notebook
with open('report.ipynb') as f:
nb = nbformat.read(f, as_version=4)
# execute notebook
ep = ExecutePreprocessor(timeout=-1, kernel_name='python3')
ep.preprocess(nb)
# export to html
html_exporter = HTMLExporter()
html_exporter.exclude_input = True
html_data, resources = html_exporter.from_notebook_node(nb)
# write to output file
with open("notebook.html", "w") as f:
f.write(html_data)诚然,与单个os.system调用相比,工作要多得多,但我感到惊讶的是,有这么少的本地python例子……
https://stackoverflow.com/questions/65502005
复制相似问题