예제 #1
0
def test_from_json_output_type(fig1, fig_type_spec, fig_type):
    fig1_json = json.dumps(fig1, **opts)
    fig1_loaded = pio.from_json(fig1_json, output_type=fig_type_spec)

    # Check return type
    assert isinstance(fig1_loaded, fig_type)

    # Check return json
    assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
예제 #2
0
def test_from_json(fig1):
    fig1_json = json.dumps(fig1, **opts)
    fig1_loaded = pio.from_json(fig1_json)

    # Check return type
    assert isinstance(fig1_loaded, go.Figure)

    # Check return json
    assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
def test_deepcopy_figure(fig1):
    fig_copied = copy.deepcopy(fig1)

    # Contents should be equal
    assert pio.to_json(fig_copied) == pio.to_json(fig1)

    # Identities should be distinct
    assert fig_copied is not fig1
    assert fig_copied.layout is not fig1.layout
    assert fig_copied.data is not fig1.data
def test_pickle_figure_subplots_round_trip(fig_subplots):
    fig_copied = pickle.loads(pickle.dumps(fig_subplots))

    # Contents should be equal
    assert pio.to_json(fig_copied) == pio.to_json(fig_subplots)

    # Should be possible to add new trace to subplot location
    fig_subplots.add_bar(y=[0, 0, 1], row=1, col=2)
    fig_copied.add_bar(y=[0, 0, 1], row=1, col=2)

    # And contents should be still equal
    assert pio.to_json(fig_copied) == pio.to_json(fig_subplots)
예제 #5
0
def test_read_json_from_filelike(fig1, fig_type_spec, fig_type):
    # Configure file-like mock
    filemock = MagicMock()
    filemock.read.return_value = pio.to_json(fig1)

    # read_json on mock file
    fig1_loaded = pio.read_json(filemock, output_type=fig_type_spec)

    # Check return type
    assert isinstance(fig1_loaded, fig_type)

    # Check loaded figure
    assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
예제 #6
0
    def test_move_nested_trace_properties(self):
        fig = go.Figure(
            data=[
                go.Bar(y=[1, 2, 3], marker={
                    "opacity": 0.6,
                    "color": "green"
                }),
                go.Scatter(x=[1, 3, 2],
                           marker={
                               "size": 30,
                               "color": [1, 1, 0]
                           }),
                go.Bar(y=[3, 2, 1],
                       marker={
                           "opacity": 0.4,
                           "color": [1, 0.5, 0]
                       }),
            ],
            layout={"barmode": "group"},
        )

        templated_fig = pio.to_templated(fig)

        expected_fig = go.Figure(
            data=[
                go.Bar(y=[1, 2, 3]),
                go.Scatter(x=[1, 3, 2], marker={"color": [1, 1, 0]}),
                go.Bar(y=[3, 2, 1], marker={"color": [1, 0.5, 0]}),
            ],
            layout={
                "template": {
                    "data": {
                        "scatter": [go.Scatter(marker={"size": 30})],
                        "bar": [
                            go.Bar(marker={
                                "opacity": 0.6,
                                "color": "green"
                            }),
                            go.Bar(marker={"opacity": 0.4}),
                        ],
                    },
                    "layout": {
                        "barmode": "group"
                    },
                }
            },
        )

        self.assertEqual(pio.to_json(templated_fig), pio.to_json(expected_fig))
예제 #7
0
def test_from_json_skip_invalid(fig1):
    dict1 = fig1.to_dict()

    # Set bad property name
    dict1["data"][0]["marker"]["bogus"] = 123

    # Set property with bad value
    dict1["data"][0]["marker"]["size"] = -1

    # Serialize to json
    bad_json = json.dumps(dict1, **opts)
    fig1_loaded = pio.from_json(bad_json, skip_invalid=True)

    # Check loaded figure
    assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
예제 #8
0
def test_read_json_from_file_string(fig1, fig_type_spec, fig_type):
    with tempfile.TemporaryDirectory() as dir_name:

        # Write json file
        path = os.path.join(dir_name, "fig1.json")
        with open(path, "w") as f:
            f.write(pio.to_json(fig1))

        # read json from file as string
        fig1_loaded = pio.read_json(path, output_type=fig_type_spec)

        # Check return type
        assert isinstance(fig1_loaded, fig_type)

        # Check loaded figure
        assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
예제 #9
0
def test_json_renderer_show_override(fig1):
    pio.renderers.default = "notebook"
    expected_bundle = {
        "application/json": json.loads(pio.to_json(fig1, remove_uids=False))
    }

    with mock.patch("IPython.display.display") as mock_display:
        pio.show(fig1, renderer="json")

    mock_display.assert_called_once_with(expected_bundle, raw=True)
예제 #10
0
def test_write_json_filelike(fig1, pretty, remove_uids):
    # Configure file-like mock
    filemock = MagicMock()

    # write_json to mock file
    pio.write_json(fig1, filemock, pretty=pretty, remove_uids=remove_uids)

    # check write contents
    expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
    filemock.write.assert_called_once_with(expected)
예제 #11
0
    def to_mimebundle(self, fig_dict):
        config = _get_jconfig(self.config)
        if config:
            fig_dict["config"] = config

        json_compatible_fig_dict = json.loads(
            to_json(fig_dict, validate=False, remove_uids=False)
        )

        return {"application/vnd.plotly.v1+json": json_compatible_fig_dict}
예제 #12
0
def test_plotly_mimetype_renderer_show(fig1, renderer):
    pio.renderers.default = renderer
    expected = {plotly_mimetype: json.loads(pio.to_json(fig1, remove_uids=False))}

    expected[plotly_mimetype]["config"] = {"plotlyServerURL": "https://plot.ly"}

    with mock.patch("IPython.display.display") as mock_display:
        pio.show(fig1)

    mock_display.assert_called_once_with(expected, raw=True)
def test_deepcopy_figure_subplots(fig_subplots):
    fig_copied = copy.deepcopy(fig_subplots)

    # Contents should be equal
    assert pio.to_json(fig_copied) == pio.to_json(fig_subplots)

    # Subplot metadata should be equal
    assert fig_subplots._grid_ref == fig_copied._grid_ref
    assert fig_subplots._grid_str == fig_copied._grid_str

    # Identities should be distinct
    assert fig_copied is not fig_subplots
    assert fig_copied.layout is not fig_subplots.layout
    assert fig_copied.data is not fig_subplots.data

    # Should be possible to add new trace to subplot location
    fig_subplots.add_bar(y=[0, 0, 1], row=1, col=2)
    fig_copied.add_bar(y=[0, 0, 1], row=1, col=2)

    # And contents should be still equal
    assert pio.to_json(fig_copied) == pio.to_json(fig_subplots)
예제 #14
0
def test_write_json_from_file_string(fig1, pretty, remove_uids):
    with tempfile.TemporaryDirectory() as dir_name:

        # Write json
        path = os.path.join(dir_name, "fig1.json")
        pio.write_json(fig1, path, pretty=pretty, remove_uids=remove_uids)

        # Open as text file
        with open(path, "r") as f:
            result = f.read()

        # Check contents that were written
        expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
        assert result == expected
예제 #15
0
def test_json_renderer_mimetype(fig1):
    pio.renderers.default = "json"
    expected = {"application/json": json.loads(pio.to_json(fig1, remove_uids=False))}

    pio.renderers.render_on_display = False

    with mock.patch("IPython.display.display") as mock_display:
        fig1._ipython_display_()

    mock_display.assert_not_called()

    pio.renderers.render_on_display = True
    with mock.patch("IPython.display.display") as mock_display:
        fig1._ipython_display_()

    mock_display.assert_called_once_with(expected, raw=True)
예제 #16
0
def test_mimetype_combination(fig1):
    pio.renderers.default = "png+jupyterlab"

    # Configure renderer so that we can use the same parameters
    # to build expected image below
    pio.renderers["png"].width = 400
    pio.renderers["png"].height = 500
    pio.renderers["png"].scale = 1

    # pdf
    image_bytes = pio.to_image(fig1,
                               format="png",
                               width=400,
                               height=500,
                               scale=1)

    image_str = base64.b64encode(image_bytes).decode("utf8")

    # plotly mimetype
    plotly_mimetype_dict = json.loads(pio.to_json(fig1, remove_uids=False))

    plotly_mimetype_dict["config"] = {
        "plotlyServerURL": _get_jconfig()["plotlyServerURL"]
    }

    # Build expected bundle
    expected = {"image/png": image_str, plotly_mimetype: plotly_mimetype_dict}

    pio.renderers.render_on_display = False

    with mock.patch("IPython.display.display") as mock_display:
        fig1._ipython_display_()

    # assert fig1._repr_mimebundle_(None, None) is None
    mock_display.assert_not_called()

    pio.renderers.render_on_display = True
    with mock.patch("IPython.display.display") as mock_display:
        fig1._ipython_display_()

    mock_display.assert_called_once_with(expected, raw=True)
예제 #17
0
 def to_mimebundle(self, fig_dict):
     value = json.loads(to_json(fig_dict, validate=False, remove_uids=False))
     return {"application/json": value}
def test_pickle_figure_round_trip(fig1):
    fig_copied = pickle.loads(pickle.dumps(fig1))

    # Contents should be equal
    assert pio.to_json(fig_copied) == pio.to_json(fig1)
예제 #19
0
def test_to_json_pretty_print(fig1):
    assert pio.to_json(fig1, remove_uids=False,
                       pretty=True) == json.dumps(fig1, **pretty_opts)
예제 #20
0
def test_to_json_validate_false(fig1):
    dict1 = fig1.to_dict()
    dict1["layout"]["bogus"] = 37

    assert pio.to_json(dict1, validate=False) == json.dumps(dict1, **opts)
예제 #21
0
def test_to_json_validate(fig1):
    dict1 = fig1.to_dict()
    dict1["layout"]["bogus"] = 37

    with pytest.raises(ValueError):
        pio.to_json(dict1)
예제 #22
0
def test_to_json_remove_uids(fig1):
    dict1 = fig1.to_dict()
    for trace in dict1["data"]:
        trace.pop("uid", None)

    assert pio.to_json(fig1) == json.dumps(dict1, **opts)
예제 #23
0
def test_to_json(fig1):
    assert pio.to_json(fig1, remove_uids=False) == json.dumps(fig1, **opts)