Ejemplo n.º 1
0
def post_save(model, os_path, contents_manager):
    """post-save hook for converting notebooks to .py scripts and html
       in a separate folder with the same name
    """
    # only do this for notebooks
    if model['type'] != 'notebook':
        return

    # only do this if we've added the special indicator file to the working directory
    cwd = os.path.dirname(os_path)
    save_procress_indicator = os.path.join(cwd, SAVE_PROCRESS_INDICATOR_FILE)
    should_convert = os.path.exists(save_procress_indicator)

    if should_convert:
        d, fname = os.path.split(os_path)
        subfolder = os.path.splitext(fname)[0]

        converter = NbConvertApp()
        converter.postprocessor = CopyToSubfolderPostProcessor(subfolder=subfolder)

        converter.export_format = 'script'
        converter.initialize(argv=[])
        converter.notebooks = [os_path]
        converter.convert_notebooks()

        converter.export_format = 'html'
        converter.initialize(argv=[])
        converter.notebooks = [os_path]
        converter.convert_notebooks()
Ejemplo n.º 2
0
def export_notebook(notebook_path: Path, config: NbAutoexportConfig):
    """Export a given notebook file given configuration.

    Args:
        notebook_path (Path): path to notebook to export with nbconvert
        config (NbAutoexportConfig): configuration
    """
    logger.info(f"nbautoexport | Exporting {notebook_path} ...")
    logger.debug(
        f"nbautoexport | Using export configuration:\n{config.json(indent=2)}")
    with cleared_argv():
        converter = NbConvertApp()
        converter.log.handlers = logger.handlers
        converter.log.setLevel(logger.level)

        for export_format in config.export_formats:
            if config.organize_by == "notebook":
                subfolder = notebook_path.stem
            elif config.organize_by == "extension":
                subfolder = export_format.value

            converter.postprocessor = CopyToSubfolderPostProcessor(
                subfolder=subfolder, export_format=export_format)
            converter.export_format = export_format.value
            converter.initialize()
            converter.notebooks = [str(notebook_path)]
            converter.convert_notebooks()
Ejemplo n.º 3
0
def export_notebook(notebook_path: Path, config: NbAutoexportConfig):
    """Export a given notebook file given configuration.

    Args:
        notebook_path (Path): path to notebook to export with nbconvert
        config (NbAutoexportConfig): configuration
    """
    with cleared_argv():
        converter = NbConvertApp()

        for export_format in config.export_formats:
            if config.organize_by == "notebook":
                subfolder = notebook_path.stem
            elif config.organize_by == "extension":
                subfolder = export_format.value

            converter.postprocessor = CopyToSubfolderPostProcessor(
                subfolder=subfolder, export_format=export_format)
            converter.export_format = export_format.value
            converter.initialize()
            converter.notebooks = [str(notebook_path)]
            converter.convert_notebooks()