Exemple #1
0
def test_export_notebook(notebooks_dir, organize_by):
    """Test that export notebook works. Explicitly write out expected files, because tests for
    get_expected_exports will compare against export_notebook.
    """
    notebook = JupyterNotebook.from_file(notebooks_dir / "the_notebook.ipynb")
    config = NbAutoexportConfig(export_formats=EXPORT_FORMATS_TO_TEST,
                                organize_by=organize_by)
    export_notebook(notebook.path, config)

    expected_exports = set()
    for fmt in EXPORT_FORMATS_TO_TEST:
        if organize_by == "extension":
            subfolder = notebooks_dir / fmt.value
        elif organize_by == "notebook":
            subfolder = notebooks_dir / notebook.name
        extension = get_extension(notebook, fmt)

        expected_exports.add(subfolder)  # subfolder
        expected_exports.add(subfolder /
                             f"{notebook.name}{extension}")  # export file

        if fmt in FORMATS_WITH_IMAGE_DIR:
            image_subfolder = subfolder / f"{notebook.name}_files"

            expected_exports.add(image_subfolder)  # image subdir
            expected_exports.add(image_subfolder /
                                 f"{notebook.name}_1_1.png")  # image file

    all_expected = {notebook.path} | expected_exports
    assert all_expected.issubset(set(notebooks_dir.glob("**/*")))
Exemple #2
0
def test_notebook_exports_generator(notebooks_dir, export_format, organize_by):
    """Test that notebook_exports_generator matches what export_notebook produces."""
    notebook = find_notebooks(notebooks_dir)[0]
    notebook_files = {
        notebooks_dir / f"{nb}.ipynb"
        for nb in EXPECTED_NOTEBOOKS
    }

    config = NbAutoexportConfig(export_formats=[export_format],
                                organize_by=organize_by)
    export_notebook(notebook.path, config)

    predicted_exports = set(
        notebook_exports_generator(notebook, export_format, organize_by))

    actual_exports = set(notebooks_dir.glob("**/*")).difference(notebook_files)
    assert predicted_exports == actual_exports
Exemple #3
0
def export(
    input: Path = typer.Argument(
        ...,
        exists=True,
        file_okay=True,
        dir_okay=True,
        writable=True,
        help="Path to notebook file or directory of notebook files to export.",
    ),
    export_formats: Optional[List[ExportFormat]] = typer.Option(
        None,
        "--export-format",
        "-f",
        show_default=True,
        help=
        ("File format(s) to save for each notebook. Multiple formats should be provided using "
         "multiple flags, e.g., '-f script -f html -f markdown'. Provided values will override "
         "existing .nbautoexport config files. If neither provided, defaults to "
         f"{DEFAULT_EXPORT_FORMATS}."),
    ),
    organize_by: Optional[OrganizeBy] = typer.Option(
        None,
        "--organize-by",
        "-b",
        show_default=True,
        help=
        ("Whether to save exported file(s) in a subfolder per notebook or per export format. "
         "Provided values will override existing .nbautoexport config files. If neither "
         f"provided, defaults to '{DEFAULT_ORGANIZE_BY}'."),
    ),
):
    """Manually export notebook or directory of notebooks.

    An .nbautoexport configuration file in same directory as notebook(s) will be used if it
    exists. Configuration options specified by command-line options will override configuration
    file. If no existing configuration option exists and no values are provided, default values
    will be used.

    The export command will not do cleaning, regardless of the 'clean' setting in an .nbautoexport
    configuration file.
    """
    if input.is_dir():
        sentinel_path = input / SAVE_PROGRESS_INDICATOR_FILE
        notebook_paths = [nb.path for nb in find_notebooks(input)]

        if len(notebook_paths) == 0:
            typer.echo(f"No notebooks found in directory [{input}]. Exiting.")
            raise typer.Exit(code=1)

    else:
        sentinel_path = input.parent / SAVE_PROGRESS_INDICATOR_FILE
        notebook_paths = [input]

    # Configuration: input options override existing sentinel file
    if sentinel_path.exists():
        typer.echo(
            f"Reading existing configuration file from {sentinel_path} ...")
        config = NbAutoexportConfig.parse_file(path=sentinel_path,
                                               content_type="application/json")

        # Overrides
        if len(export_formats) > 0:
            typer.echo(
                f"Overriding config with specified export formats: {export_formats}"
            )
            config.export_formats = export_formats
        if organize_by is not None:
            typer.echo(
                f"Overriding config with specified organization strategy: {export_formats}"
            )
            config.organize_by = organize_by
    else:
        typer.echo(
            "No configuration found. Using command options as configuration ..."
        )
        if len(export_formats) == 0:
            typer.echo(
                f"No export formats specified. Using default: {DEFAULT_EXPORT_FORMATS}"
            )
            export_formats = DEFAULT_EXPORT_FORMATS
        if organize_by is None:
            typer.echo(
                f"No organize-by specified. Using default: {DEFAULT_ORGANIZE_BY}"
            )
            organize_by = DEFAULT_ORGANIZE_BY
        config = NbAutoexportConfig(export_formats=export_formats,
                                    organize_by=organize_by)

    for notebook_path in notebook_paths:
        export_notebook(notebook_path, config=config)