Beispiel #1
0
def test_load_saved_chart(unittest):
    import dtale.views as views

    df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6], c=[7, 8, 9]))
    with app.test_client() as c:
        with ExitStack():
            df, _ = views.format_data(df)
            build_data_inst({c.port: df})

            input_data = {
                "data_id": str(c.port),
                "query": None,
                "chart_type": "line",
                "agg": "sum",
                "x": "a",
                "y": ["b"],
                "yaxis": {},
                "cpg": False,
                "cpy": False,
                "animate": False,
                "trendline": False,
            }
            params = {
                "output": (
                    "..saved-chart-div-1.style...saved-chart-1.children...prev-saved-chart-config-1.data."
                    "..saved-chart-header-1.children.."
                ),
                "inputs": [ts_builder("saved-chart-config-1")],
                "state": [
                    {
                        "id": "saved-chart-config-1",
                        "property": "data",
                        "value": input_data,
                    },
                    {
                        "id": "prev-saved-chart-config-1",
                        "property": "data",
                        "value": None,
                    },
                ],
            }
            response = c.post("/dtale/charts/_dash-update-component", json=params)
            response = response.json["response"]
            assert response["saved-chart-div-1"]["style"]["display"] == "block"

            params["state"][1]["value"] = input_data
            response = c.post("/dtale/charts/_dash-update-component", json=params)
            assert response.status_code == 204

            params["state"][0]["value"] = None
            response = c.post("/dtale/charts/_dash-update-component", json=params)
            response = response.json["response"]
            assert response["saved-chart-div-1"]["style"]["display"] == "none"
Beispiel #2
0
def test_load_drilldown_content(custom_data):
    import dtale.views as views

    with app.test_client() as c:
        custom_data.loc[:, "Col4"] = 4
        df, _ = views.format_data(custom_data)
        build_data_inst({c.port: df})
        fig_data_outputs = (
            "..drilldown-content-1.children...drilldown-x-input-1.style..")
        inputs = {
            "id": "input-data",
            "property": "data",
            "value": {
                "data_id": c.port,
                "chart_type": "bar",
                "x": "security_id",
                "y": ["Col0"],
                "z": None,
                "group": None,
                "agg": None,
                "window": None,
                "rolling_comp": None,
                "animate_by": "date",
            },
        }
        params = build_dash_request(
            fig_data_outputs,
            "chart-click-data-1.modified_timestamp",
            [
                ts_builder("chart-click-data-1"),
                {
                    "id": "drilldown-chart-type-1",
                    "property": "value",
                    "value": None,
                },
                {
                    "id": "drilldown-x-dropdown-1",
                    "property": "value",
                    "value": None,
                },
            ],
            [
                inputs,
                {
                    "id": "chart-input-data",
                    "property": "data",
                    "value": {
                        "cpg": False,
                        "barmode": "group",
                        "barsort": None
                    },
                },
                {
                    "id": "yaxis-data",
                    "property": "data",
                    "value": {}
                },
                {
                    "id": "map-input-data",
                    "property": "data",
                    "value": {}
                },
                {
                    "id": "chart-click-data-1",
                    "property": "data",
                    "value": {}
                },
                {
                    "id": "drilldown-toggle",
                    "property": "on",
                    "value": False
                },
            ],
        )
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert response.get_json() is None
        assert response.status_code == 204
        params["state"][-1]["value"] = True
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert response.get_json() is None
        assert response.status_code == 204
        params["state"][0]["value"]["agg"] = "mean"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert response.get_json() is None
        assert response.status_code == 204
        params["inputs"][-1]["value"] = "security_id"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        response = response.get_json()["response"]
        assert response["drilldown-content-1"]["children"] is None
        assert response["drilldown-x-input-1"]["style"]["display"] == "none"
        params["state"][-2]["value"] = {
            "points": [{
                "x":
                100000,
                "y":
                1.23,
                "customdata":
                pd.Timestamp(df.date.values[0]).strftime("%Y%m%d"),
            }]
        }
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        exception = print_traceback(response,
                                    chart_key="drilldown-content-1",
                                    return_output=True)
        assert "NotImplementedError: chart type: None" in exception
        params["inputs"][-2]["value"] = "histogram"
        response = c.post("/dtale/charts/_dash-update-component", json=params)

        def _chart_title(resp, histogram=False):
            if histogram:
                return resp.get_json()["response"]["drilldown-content-1"][
                    "children"]["props"]["figure"]["layout"]["title"]["text"]
            return resp.get_json(
            )["response"]["drilldown-content-1"]["children"]["props"][
                "children"][1]["props"]["figure"]["layout"]["title"]["text"]

        assert _chart_title(response,
                            True) == "Histogram of Col0 (1 data points)"
        params["inputs"][-2]["value"] = "bar"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response) == "Col0 by security_id"

        params["inputs"][-2]["value"] = "histogram"
        params["state"][0]["value"]["chart_type"] = "3d_scatter"
        params["state"][0]["value"]["y"] = "Col4"
        params["state"][0]["value"]["z"] = "Col0"
        params["state"][-2]["value"]["points"][0]["y"] = 4
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response,
                            True) == "Histogram of Col0 (1 data points)"
        params["inputs"][-2]["value"] = "bar"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response) == "Col0 by security_id"

        params["inputs"][-2]["value"] = "histogram"
        params["state"][0]["value"]["chart_type"] = "heatmap"
        date_val = pd.Timestamp(
            df[(df.security_id == 100000)
               & (df.Col4 == 4)].date.values[0]).strftime("%Y%m%d")
        params["state"][-2]["value"] = {
            "points": [{
                "x":
                100000,
                "y":
                4,
                "z":
                1,
                "text":
                "date: {}<br>security_id: 100000<br>Col4: 4<br>Col0: 1".format(
                    date_val),
                "customdata":
                date_val,
            }]
        }
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response,
                            True) == "Histogram of Col0 (1 data points)"
        params["inputs"][-2]["value"] = "bar"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response) == "Col0 by security_id"

        params["inputs"][-2]["value"] = "histogram"
        params["state"][0]["value"]["chart_type"] = "maps"
        params["state"][-3]["value"] = {
            "map_type": "choropleth",
            "loc": "security_id",
            "map_val": "Col0",
        }
        params["state"][-2]["value"]["points"][0]["location"] = 100000
        params["state"][-2]["value"]["points"][0]["z"] = 1.23
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response,
                            True) == "Histogram of Col0 (1 data points)"
        params["inputs"][-2]["value"] = "bar"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response) == "Col0 by security_id"

        params["inputs"][-2]["value"] = "histogram"
        params["state"][-3]["value"] = {
            "map_type": "scattergeo",
            "lat": "security_id",
            "lon": "Col4",
            "map_val": "Col0",
        }
        params["state"][-2]["value"]["points"][0]["lat"] = 100000
        params["state"][-2]["value"]["points"][0]["lon"] = 4
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response,
                            True) == "Histogram of Col0 (1 data points)"
        params["inputs"][-2]["value"] = "bar"
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        assert _chart_title(response) == "Col0 by lat_lon"