Jupyter-将当前ipynb文件导出到笔记本中的html

时间:2018-08-13 11:04:30

标签: python ipython jupyter-notebook jupyter

我正在尝试在jupyter笔记本中编写python代码,这会将整个最新笔记本导出到html / pdf。

我知道我可以做这样的事情:

import subprocess
subprocess.call("jupyter nbconvert notebook.ipynb")

但这需要文件名,这在笔记本电脑内部是未知的

我看到了很多获取笔记本文件名的“技巧”,例如: How do I get the current IPython Notebook name 但我更愿意找到一个更好的解决方案。

是否存在将当前正在运行的笔记本导出为html / pdf文件的聪明方法?

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用库 papermill ,该库将使用提取的参数(基本上是您在笔记本外部定义的笔记本名称 )执行笔记本

Python程序

pm.execute_notebook(
   'notebooks/notebookA.ipynb', # notebook to execute
   'notebooks/temp.ipynb', # temporary notebook that will contains the outputs and will be used to save HTML
   report_mode=True, # To hide ingested parameters cells, but not working for me
   parameters=dict(filename_ipynb='notebooks/temp.ipynb', 
                   filename_html='output/notebookA.html')
)

/notebooks/notebookA.ipynb

(要插入为笔记本的最后一个单元格)

import os
os.system('jupyter nbconvert --output ../' + filename_html + ' --to html ' + filename_ipynb)

只是一件事: 它正在添加一个单元格以设置摄取的参数。它应该可以使用report_mode = False来隐藏它,但是即使它应该:https://github.com/nteract/papermill/issues/253

相关问题