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 convert_tour_nb_to_document():
    app = NbConvertApp()
    app.initialize()
    app.notebooks = ['../Skills-ML Tour.ipynb']
    app.export_format = 'markdown'
    app.output_base = 'skills_ml_tour.md'
    app.writer.build_directory = 'sources/'
    app.convert_notebooks()
Ejemplo n.º 4
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='''
        Make (and run) a jupyter notebook visualizing the results of an output
        file from fixed_run.py.'''.strip())
    parser.add_argument('fname')
    parser.add_argument('--key', default='df')
    parser.add_argument('out_name', nargs='?')
    parser.add_argument('-n', type=int)
    parser.add_argument('--level', type=float, default=.1)
    parser.add_argument('--kernel-name')
    parser.add_argument('--force', '-f', action='store_true')
    args = parser.parse_args()

    if args.out_name is None:
        assert args.fname.endswith('.h5')
        base = args.fname[:-3]
        args.out_name = base + '.ipynb'
    else:
        d = os.path.dirname(args.out_name)
        if d and not os.path.isdir(d):
            os.makedirs(d)

    if not args.force and os.path.exists(args.out_name):
        parser.exit(
            "Output {} already exists; use --force to overwrite.".format(
                args.out_name))
    del args.force

    if args.n is None:
        match = re.search('/n(\d+)\.h5$', args.fname)
        if match:
            args.n = int(match.group(1))
        else:
            parser.error('-n must be specified unless filename has it')

    make_notebook(**vars(args))
    nbc = NbConvertApp()
    nbc.initialize([
        '--to=notebook', '--execute', '--output',
        os.path.basename(args.out_name), args.out_name
    ])
    nbc.convert_notebooks()
Ejemplo n.º 5
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()
Ejemplo n.º 6
0
def main():
    p = ArgumentParser()
    p.add_argument("ipynb_notebook_file")
    p.add_argument("-o", "--out")
    p.add_argument("-d", "--default-slide-type", default="skip")
    p.add_argument("--include-input-prompt", action="store_true")
    p.add_argument("--include-output-prompt", action="store_true")
    args = p.parse_args()
    # https://nbconvert.readthedocs.io/en/latest/config_options.html?highlight=TemplateExporter.exclude
    TemplateExporter.exclude_input_prompt = not args.include_input_prompt
    TemplateExporter.exclude_output_prompt = not args.include_output_prompt

    with open(args.ipynb_notebook_file) as nb, FakeSysArgv(
    ), tempfile.NamedTemporaryFile(suffix=".ipynb", mode="w") as out_nb:
        nb_content = json.load(nb)
        for i, cell in enumerate(nb_content["cells"]):
            nb_content["cells"][i].setdefault("metadata", {})
            nb_content["cells"][i]["metadata"].setdefault("slideshow", {})
            nb_content["cells"][i]["metadata"].setdefault("tags", [])
            if cell["cell_type"] == "markdown":
                regex = MARKDOWN_CODE_REGEX
            elif cell["cell_type"] == "code":
                regex = PY_CODE_REGEX
            slide_type = args.default_slide_type
            tags = []
            # check the first line
            if not cell["source"]:
                continue
            m = regex.match(cell["source"][0])
            if m:
                slideshow_config = m.group(1)
                slideshow_config_items = slideshow_config.split()
                slide_type = slideshow_config_items[0]
                if slide_type not in ALLOWED_SLIDE_TYPES:
                    raise ValueError(
                        "unknown slide type: {}".format(slide_type))
                # find tags in format "tags=tag1,tag2,..."
                for item in slideshow_config_items:
                    if not item.startswith("tags="):
                        continue
                    item_tags = item[len("tags="):].split(",")
                    for tag in item_tags:
                        # add only new tags, just in case
                        if tag not in cell["metadata"].get("tags", []):
                            tags.append(tag)
                nb_content["cells"][i]["source"] = cell["source"][
                    1:]  # remove the first line

            nb_content["cells"][i]["metadata"]["slideshow"][
                "slide_type"] = slide_type
            nb_content["cells"][i]["metadata"]["tags"] += tags
        json.dump(nb_content, out_nb)
        out_nb.flush()
        converter = NbConvertApp()
        converter.notebooks = [out_nb.name]
        converter.export_format = "slides"
        if not args.out:
            converter.postprocessor_class = "serve"
        converter.initialize()
        converter.convert_notebooks()

        if args.out:
            base = os.path.splitext(out_nb.name)[0]
            shutil.copy(base + ".slides.html", args.out)