我会自己回答。
from IPython.display import Javascript
from nbconvert import HTMLExporter
def save_notebook():
display(
Javascript("IPython.notebook.save_notebook()"),
include=['application/javascript']
)
def output_HTML(read_file, output_file):
import codecs
import nbformat
exporter = HTMLExporter()
# read_file is '.ipynb', output_file is '.html'
output_notebook = nbformat.read(read_file, as_version=4)
output, resources = exporter.from_notebook_node(output_notebook)
codecs.open(output_file, 'w', encoding='utf-8').write(output)
在笔记本的最后一个单元格,类似于:import time
save_notebook()
time.sleep(3)
current_file = 'GMM.ipynb'
output_file = 'output_file.html'
output_HTML(current_file, output_file)
在同一目录下,您应该能够找到一个脚本,其中包含jupyter-notebook.exe
文件。它的名称是jupyter-nbconvert.exe
。像这样运行它:
./jupyter-nbconvert.exe --to html 'path/to/nb.ipynb'`
首先打开控制台并进入笔记本所在的目录。
第二步输入以下命令:
ipython nbconvert --to HTML your_notebook.ipynb
接下来,你将会得到一个名为“your_notebook.html”的新文件。就这样。
你可以在这里查看更多信息。
执行此代码片段即可
!!jupyter nbconvert *.ipynb
!!jupyter nbconvert MY_FILENAME.ipynb
只转换一个笔记本。请注意,它会输出 MY_FILENAME.html
。似乎没有一种方法可以指定输出文件名,所以在生成 HTML 文件后,我使用 os.rename('MY_FILENAME.html', 'new_path/new_name.html')
进行重命名。 - MD004!!jupyter nbconvert MY_FILENAME.ipynb --to html
。在运行之前确保保存笔记本。 - MD004a = 1
command = f'jupyter nbconvert Untitled.ipynb --output test_{a}.html'
subprocess.call(command)
from IPython.display import Javascript
- KeepLearning