Automatically save .py and .html files in Jupyter notebook.

Motivation

I am creating a programming summary note with jupyter notebook, but I want to save a good-looking (or rather, viewable without starting from the terminal) html file at the same timing as the automatic save of jupyter notebook.

Referenced site

IPython and Jupyter Notebooks: Automatically Export .py and .html

Source code

Create the following code under ~ ./. Jupyter /.

jupyter_notebook_config.py


# Based off of https://github.com/jupyter/notebook/blob/master/docs/source/extending/savehooks.rst

import io
import os
from notebook.utils import to_api_path

_script_exporter = None
_html_exporter = None

def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter
    from nbconvert.exporters.html import HTMLExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    global _html_exporter
    if _html_exporter is None:
        _html_exporter = HTMLExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

    # save html
    base, ext = os.path.splitext(os_path)
    script, resources = _html_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving html /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
c.FileContentsManager.post_save_hook = script_post_save

Save it, launch jupyter notebook normally, and make sure the script is saved. :)

Supplement

If you don't need .py, comment out the script.

Thank you to the website administrator !!!

Recommended Posts

Automatically save .py and .html files in Jupyter notebook.
Display HTML in Jupyter notebook
Convert jupyter notebook .ipynb files to python executable .py files
Multiprocessing error in Jupyter Notebook
Embed wav files in Jupyter
Automatically opens in a new window when launching Jupyter Notebook
View photos in Python and html
Manipulate files and folders in Python
Generate Jupyter notebook ".ipynb" in Python
Embed your favorite HTML in Jupyter
Jupyter Notebook: 4 banal tips and tricks
Save and retrieve files with Pepper
View graphs inline in Jupyter Notebook
Export and output files in Python
Easy way to enter and execute AtCoder test cases in Jupyter Notebook
Allow Jupyter Notebook to embed audio data in HTML tables for playback
Memory leak in Python Jupyter Lab (Notebook)?
Use Jupyter Lab and Jupyter Notebook with EC2
Create and run embulk config in Jupyter
Edit and save read-only files with vim
How to execute commands in jupyter notebook
Jupyter Notebook basic operations and shortcut keys
Read and write JSON files in Python
Linking python and JavaScript with jupyter notebook
Build a PYNQ environment on Ultra96 V2 and log in to Jupyter Notebook
When Html cannot be output with Jupyter Notebook
12. Save the first column in col1.txt and the second column in col2.txt
Automatically create word and excel reports in python
View Selenium screenshots in Jupyter without saving files
Reading and writing CSV and JSON files in Python
Coexistence of Anaconda 2 and Anaconda 3 in Jupyter + Bonus (Julia)
Error running Jupyter Notebook in Anaconda virtual environment
(Bottom) Cooperation between Jupyter Notebook and running Excel
Use ipywidgets in jupyter notebook to interactively manipulate parameters and also try image processing