Programmatically Export to HTML from Within Notebook With Custom Output Path

I have a use case in which I have a notebook intended for use by non-programmers. All they have to do is make sure all of the cells run. I’d like to get the last cell to write an HTML copy of the notebook to disk without the user needing to modify anything in the cell.

With a regular notebook, my initial attempt was to use JavaScript to discover the notebook path…


Jupyter.notebook.kernel.execute("window_loc = '" + window.location + "'")

…extract the file name…

current_file: str = window_loc.split("/")[-1]

…and then issue bash commands to convert the file.

!jupyter nbconvert $current_file --to html --output=$my_custom_output_path --output-dir=$my_out_dir

…where $my_custom_output_path and $my_out_dir are generated on the fly from notebook content.

This has proven brittle, insofar as the HTML copies were sometimes incomplete. Moreover, the same approach doesn’t seem to work in JupyterLab. Has anyone discovered a single language answer to this problem (ideally all python)? I’m all ears on better approaches.

Is the use case on a JupyterHub by any chance? That offers additional possibilities, I think.

Is a notebook interface the best use here? Have you considered Voila or Appmode?
And just exporting what you need as an HTML file into the current working directory which will be where you are working anyway, usually where the notebook is?

Related to a notebook saving itself:

In fact it is JupyterHub. What did you have in mind?

I hear you on a better UI. I’d love to have the space to create a new one, but it’s just not in the cards at the moment. For right now, I just need someway of writing to some place outside of the current working directory. In this scenario, multiple users have their own working directories, but the HTML copies must be written to one common directory.

Does your JupyterHub allow access to the os.environ.items()/ envs from inside the notebook? See example code in this post and related discussion above it.