def test_regex_replace_nb_source():
    """Test regex replacing notebook source."""
    notebook = create_notebook()
    notebook.cells.extend(
        [
            prepare_cell(
                {"metadata": {}, "outputs": [], "source": ["print(1)\n", "print(2)"]}
            ),
            prepare_cell(
                {"metadata": {}, "outputs": [], "source": ["print(1)\n", "print(2)"]}
            ),
        ]
    )
    new_notebook = regex_replace_nb(
        notebook, [("/cells/0/source", "p", "s"), ("/cells/1/source", "p", "t")]
    )
    new_notebook.nbformat_minor = None
    assert mapping_to_dict(new_notebook) == {
        "cells": [
            {"metadata": {}, "outputs": [], "source": "srint(1)\nsrint(2)"},
            {"metadata": {}, "outputs": [], "source": "trint(1)\ntrint(2)"},
        ],
        "metadata": {},
        "nbformat": 4,
        "nbformat_minor": None,
    }
示例#2
0
def test_regex_replace_nb_output():
    """Test regex replacing notebook output."""
    notebook = create_notebook()
    notebook.cells.extend([
        prepare_cell({
            "metadata": {},
            "outputs": [{
                "name": "stdout",
                "output_type": "stream",
                "text": ["2019-08-11\n"],
            }],
            "source": ["from datetime import date\n", "print(date.today())"],
        })
    ])
    new_notebook = regex_replace_nb(
        notebook,
        [("/cells/0/outputs/0", r"\d{2,4}-\d{1,2}-\d{1,2}", "DATE-STAMP")])
    output = mapping_to_dict(new_notebook)
    output.pop("nbformat_minor")
    assert output == {
        "cells": [{
            "metadata": {},
            "outputs": [{
                "name": "stdout",
                "output_type": "stream",
                "text": ["DATE-STAMP\n"],
            }],
            "source":
            "from datetime import date\nprint(date.today())",
        }],
        "metadata": {},
        "nbformat":
        4,
    }
示例#3
0
def test_beautifulsoup(data_regression):
    """Test applying beautifulsoup to html outputs."""
    notebook = create_notebook()
    notebook.cells.extend([
        prepare_cell({
            "cell_type":
            "code",
            "execution_count":
            1,
            "metadata": {},
            "outputs": [{
                "data": {
                    "text/html": [
                        "\n",
                        '<div class="section" id="submodules">\n',
                        '    <h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this headline">¶</a></h2>\n',  # noqa: E501
                        "</div>",
                    ],
                    "text/plain": ["<IPython.core.display.HTML object>"],
                },
                "execution_count": 1,
                "metadata": {},
                "output_type": "execute_result",
            }],
            "source": [""],
        }),
        prepare_cell({
            "cell_type":
            "code",
            "execution_count":
            2,
            "metadata": {},
            "outputs": [{
                "data": {
                    "image/svg+xml": [
                        '<svg height="100" width="100">\n',
                        '  <circle cx="50" cy="50" fill="red" r="40" stroke="black" stroke-width="3"/></svg>\n',  # noqa: E501
                        "",
                    ],
                    "text/plain": ["<IPython.core.display.SVG object>"],
                },
                "execution_count": 2,
                "metadata": {},
                "output_type": "execute_result",
            }],
            "source": [""],
        }),
    ])

    new_notebook, resources = beautifulsoup(notebook, {})
    assert resources["beautifulsoup"] == [
        "/cells/0/outputs/0/text/html",
        "/cells/1/outputs/0/image/svg+xml",
    ]
    output = mapping_to_dict(new_notebook)
    output["nbformat_minor"] = 2
    data_regression.check(output)
示例#4
0
def test_notebooks_unequal(data_regression):
    initial = nbformat.read(os.path.join(path, "raw_files",
                                         "different_outputs.ipynb"),
                            as_version=4)
    final = nbformat.read(os.path.join(path, "raw_files",
                                       "different_outputs_altered.ipynb"),
                          as_version=4)
    diff = diff_notebooks(initial, final)
    data_regression.check(mapping_to_dict(diff))
示例#5
0
def test_blacken_code(data_regression):
    """Test blacken unformatted code."""
    notebook = create_notebook()
    notebook.cells.append(
        prepare_cell({
            "cell_type":
            "code",
            "execution_count":
            1,
            "metadata": {},
            "outputs": [],
            "source":
            textwrap.dedent("""\
                    for i in range(5):
                      x=i
                      a ='123'# comment
                    """),
        }))

    new_notebook, _ = blacken_code(notebook, {})
    output = mapping_to_dict(new_notebook)
    output["nbformat_minor"] = 2
    data_regression.check(output)
示例#6
0
def test_regex_replace_nb_with_wildcard():
    """Test regex replacing notebook values."""
    notebook = create_notebook()
    notebook.cells.extend([
        prepare_cell({
            "metadata": {},
            "outputs": [],
            "source": ["print(1)\n", "print(2)"]
        }),
        prepare_cell({
            "metadata": {},
            "outputs": [],
            "source": ["print(1)\n", "print(2)"]
        }),
    ])
    new_notebook = regex_replace_nb(notebook, [("/cells/*/source", "p", "s"),
                                               ("/cells/0/source", "t", "y")])

    output = mapping_to_dict(new_notebook)
    output.pop("nbformat_minor")
    assert output == {
        "cells": [
            {
                "metadata": {},
                "outputs": [],
                "source": "sriny(1)\nsriny(2)"
            },
            {
                "metadata": {},
                "outputs": [],
                "source": "srint(1)\nsrint(2)"
            },
        ],
        "metadata": {},
        "nbformat":
        4,
    }