简体   繁体   中英

Jupyter - export current ipynb file to html within the notebook

I'm trying to write python code in my jupyter notebook, that will export the whole current notebook to html/pdf.

I know I can do something like that:

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

But that requires the file name, which is not known inside the notebook .

I saw many "hacks" to get the notebook file name, like this one: How do I get the current IPython Notebook name but I prefer to find a better solution.

Is there a smart way to export the current running notebook to a html/pdf file?

Thanks.

You can use the library papermill , that will execute the notebook with ingested parameters (basically your notebook name that you define outside your notebook )

Python program

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

(To insert as the last cell of your notebook)

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

Just a thing: it is adding a cell to set the parameters ingested. It should be able to hide it with report_mode=False but somehow it doesn't work, even though it should: https://github.com/nteract/papermill/issues/253

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM