Add ability to watch notebook files and convert automatically
My use case is using Jupyter Notebooks to create a slideshow. I am then periodically running:
jupyter nbconvert myfile.ipynb --to slides --post serve
This automatically opens a browser tab with the rendered slideshow in it. When I have made a change to the notebook file, I then have to close that browser tab, Ctrl-C on the nbconvert process in the terminal and run it again.
I would like to able able to run something like:
jupyter nbconvert myfile.ipynb --to slides --post serve --watch
which would track changes to the notebook file, automatically rerun the conversion and reload the slideshow in the browser to show the latest changes.
This would mean that I could simply leave that process running in the background while I am developing the material, and only have to deal with switching between the two windows to see the changes almost live.
Partial workaround: use entr for reloading. nbconvert doesn't support serving without opening a new browser tab, which means you will have another browser tab opened on each reload.
ls *.ipynb | entr -r jupyter nbconvert myfile.ipynb --to slides --post serve
This can likely be done with Jupyter notebook's post save hooks.
@teodorlu did you find a way to have it not open a new browser tab at each save in the notebook? Thanks 🙏
One can prevent the browser from opening with the following config setting.
c.ServePostProcessor.open_in_browser=False
@t-makaro I put the line in jupyter_notebook_config.py. But the browser continues opening tabs every time a file is saved.
The workaround I came up with that prevents always open a new tab is:
- open the output (
*.slides.html) manually in your browser - periodically run nbconvert with
watch -n 1 jupyter nbconvert tmp.ipynb --to slides(note: without--post serve) * - refresh the page when needed
* (rebuild the slides with entr or similar tools is of course better, but watch is available by default almost every linux distro)
edit: if you use vscode, you can use Live Server to serve the html which will automatically refresh the page if the html has changed. And here you go: a complete automated solution!