def test_nbexec(tmp_path: Path) -> None: """Run ``nbexec`` to execute a temporary notebook file.""" for cell in nbexec(make_notebook(tmp_path)).cells: if cell.cell_type == "code": assert cell.execution_count for output in cell.outputs: assert output
def test_nbraze(tmp_path: Path): """Extract code and markdown files from the cells of an input notebook.""" fdict = nbraze(make_notebook(tmp_path)) assert [Path(f).suffix for f in fdict] == [".md", ".py", ".md", '.json'] assert fdict["notebook_cell0.md"].startswith("# Background\nMatplotlib") assert fdict["notebook_cell1.py"].startswith("import numpy as np\n") assert fdict["notebook_cell2.md"].startswith("# Discussion\nMatplotlib")
def test_nbraze_cli(tmp_path: Path): """Extract code and markdown files from the cells of an input notebook.""" runner = CliRunner() with runner.isolated_filesystem(): runner.invoke(nbraze_cli.nbraze_cli, make_notebook(tmp_path)) assert Path("notebook_cell0.md").read_text().startswith("# Background") assert Path("notebook_cell1.py").read_text().startswith("import numpy") assert Path("notebook_cell2.md").read_text().startswith("# Discussion")
def test_nbconv_file_contents(tmp_path: Path): """Run ``nbconv`` with the ``exporter`` or ``out_file`` argument.""" nb = make_notebook(tmp_path) assert nbconv(in_file=nb, exporter="html")[1].startswith("<!DOCTYPE html>") assert nbconv(in_file=nb, out_file="o.html")[1].startswith("<!DOCTYPE html") assert nbconv(in_file=nb, out_file="o.___")[1].startswith("<!DOCTYPE html>") assert nbconv(in_file=nb, out_file="o.asc")[1].startswith("\n[[background]") assert nbconv(in_file=nb, exporter="rst")[1].startswith("\nBackground\n") assert nbconv(in_file=nb, out_file="o.rst")[1].startswith("\nBackground\n")
def test_nbdeck(tmp_path: Path): """ Set up a Jupyter notebook to be viewed as or converted into slides.""" cells = nbdeck(make_notebook(tmp_path)).cells c = 0 for cell in cells: if cell.cell_type == "markdown" and cell.source.startswith("#"): c += 1 assert cell.metadata.slideshow == {"slide_type": "slide"} assert c == 2 assert len(cells) == 3
def test_nbconv_cli(tmp_path: Path) -> None: """Convert ``tempfiles`` with each exporter in ``exporters``.""" runner = CliRunner() with runner.isolated_filesystem(): nb = make_notebook(tmp_path) runner.invoke(nbconv_cli.nbconv_cli, [nb, "-e", "html"]) assert Path("notebook.html").read_text().startswith("<!DOCTYPE html>") runner.invoke(nbconv_cli.nbconv_cli, [nb, "-o", "report.html"]) assert Path("report.html").read_text().startswith("<!DOCTYPE html>\n") runner.invoke(nbconv_cli.nbconv_cli, [nb, "-o", "report.adoc"]) assert Path("report.adoc").read_text().startswith("\n[[background]]")
def test_nbexec_cli_out(tmp_path: Path) -> None: """Run nbexec to execute a temporary notebook with a custom filename.""" runner = CliRunner() with runner.isolated_filesystem(): nb = make_notebook(tmp_path) runner.invoke(nbexec_cli.nbexec_cli, [nb, "-o", "exe.ipynb"]) cells = nbformat.read("exe.ipynb", as_version=4).cells for cell in cells: if cell.cell_type == "code": assert cell.execution_count for output in cell.outputs: assert output
def test_nbexec_cli(tmp_path: Path) -> None: """Run nbexec to execute a temporary notebook file.""" runner = CliRunner() with runner.isolated_filesystem(): nb = make_notebook(tmp_path) result = runner.invoke(nbexec_cli.nbexec_cli, nb) cells = nbformat.reads(result.output, as_version=4).cells for cell in cells: if cell.cell_type == "code": assert cell.execution_count for output in cell.outputs: assert output
def test_nbdeck_cli_out(tmp_path: Path): """Set up a Jupyter notebook to be viewed as or converted into slides.""" runner = CliRunner() with runner.isolated_filesystem(): nb = make_notebook(tmp_path) runner.invoke(nbdeck_cli.nbdeck_cli, [nb, "-o", "slides.ipynb"]) cells = nbformat.read("slides.ipynb", as_version=4).cells c = 0 for cell in cells: if cell.cell_type == "markdown" and cell.source.startswith("#"): c += 1 assert cell.metadata.slideshow == {"slide_type": "slide"} assert c == 2 assert len(cells) == 3
def test_nbdeck_cli(tmp_path: Path): """Print a notebook that can be viewed as or converted into slides.""" runner = CliRunner() with runner.isolated_filesystem(): nb = make_notebook(tmp_path) result = runner.invoke(nbdeck_cli.nbdeck_cli, nb) cells = nbformat.reads(result.output, as_version=4).cells c = 0 for cell in cells: if cell.cell_type == "markdown" and cell.source.startswith("#"): c += 1 assert cell.metadata.slideshow == {"slide_type": "slide"} assert c == 2 assert len(cells) == 3
def test_nbconv(exporters, tmp_path: Path) -> None: """Convert a temporary notebook with each exporter in ``exporters``.""" nb = make_notebook(tmp_path) assert nbconv(in_file=nb, exporter=exporters)[0].endswith("." + exporters) assert nbconv(in_file=nb)[0].endswith(".html")
def test_raises(not_exporters, tmp_path: Path) -> None: """Make sure a ``ValueError`` is raised if ``nbconv`` gets a bad exporter.""" nb = make_notebook(tmp_path) with pytest.raises(ValueError): nbconv(in_file=nb, exporter=not_exporters) nbconv(in_file=nb, out_file="out." + not_exporters)