Esempio n. 1
0
def test_build_query():
    with ExitStack() as stack:
        settings = {'1': {'columnFilters': {'foo': {'query': 'foo == 1'}}}}
        stack.enter_context(mock.patch('dtale.global_state.SETTINGS',
                                       settings))
        assert utils.build_query('1') == 'foo == 1'
Esempio n. 2
0
    def load_drilldown_content(
        _click_data_ts,
        drilldown_type,
        drilldown_x,
        pathname,
        inputs,
        chart_inputs,
        yaxis_data,
        map_data,
        click_data,
        drilldowns_on,
    ):
        if not drilldowns_on:
            raise PreventUpdate
        all_inputs = combine_inputs(
            dash_app, inputs, chart_inputs, yaxis_data, map_data
        )
        agg = all_inputs.get("agg") or "raw"
        chart_type = all_inputs.get("chart_type")
        frame_col = all_inputs.get("animate_by")
        all_inputs.pop("animate_by", None)
        if agg == "raw":
            raise PreventUpdate
        if drilldown_x is None and chart_type != "maps":
            raise PreventUpdate
        if click_data:
            click_point = next((p for p in click_data.get("points", [])), None)
            if click_point:
                data_id = get_data_id(pathname)
                curr_settings = global_state.get_settings(data_id) or {}
                query = build_query(
                    data_id, all_inputs.get("query") or curr_settings.get("query")
                )
                x_col = all_inputs.get("x")
                y_col = next((y2 for y2 in make_list(all_inputs.get("y"))), None)
                if chart_type in ZAXIS_CHARTS:
                    x, y, z, frame = (
                        click_point.get(p) for p in ["x", "y", "z", "customdata"]
                    )
                    if chart_type == "heatmap":
                        click_point_vals = {}
                        for dim in click_point["text"].split("<br>"):
                            prop, val = dim.split(": ")
                            click_point_vals[prop] = val
                        x, y, frame = (
                            click_point_vals.get(p) for p in [x_col, y_col, frame_col]
                        )
                    point_filter = {x_col: x, y_col: y}
                    if frame_col:
                        point_filter[frame_col] = frame
                    if drilldown_type == "histogram":
                        z_col = next(
                            (z2 for z2 in make_list(all_inputs.get("z"))), None
                        )
                        hist_chart = build_histogram(
                            data_id, z_col, query, point_filter
                        )
                        return hist_chart, dict(display="none")
                    else:
                        xy_query = build_group_inputs_filter(
                            global_state.get_data(data_id),
                            [point_filter],
                        )
                        if not query:
                            query = xy_query
                        else:
                            query = "({}) and ({})".format(query, xy_query)
                        all_inputs["query"] = query
                        all_inputs["chart_type"] = drilldown_type
                        all_inputs["agg"] = "raw"
                        all_inputs["modal"] = True
                        all_inputs["x"] = drilldown_x
                        all_inputs["y"] = [all_inputs["z"]]
                        chart, _, _ = build_chart(get_data_id(pathname), **all_inputs)
                        return chart, None

                elif chart_type == "maps":
                    map_type = all_inputs.get("map_type")
                    point_filter = {}
                    if frame_col:
                        point_filter[frame_col] = click_point["customdata"]
                    if map_type == "choropleth":
                        point_filter[all_inputs["loc"]] = click_point["location"]
                    elif map_type == "scattergeo":
                        lat, lon = (click_point.get(p) for p in ["lat", "lon"])
                        point_filter[all_inputs["lat"]] = lat
                        point_filter[all_inputs["lon"]] = lon
                    map_val = all_inputs["map_val"]
                    if drilldown_type == "histogram":
                        hist_chart = build_histogram(
                            data_id, map_val, query, point_filter
                        )
                        return hist_chart, dict(display="none")
                    else:
                        map_query = build_group_inputs_filter(
                            global_state.get_data(data_id),
                            [point_filter],
                        )
                        if not query:
                            query = map_query
                        else:
                            query = "({}) and ({})".format(query, map_query)
                        all_inputs["query"] = query
                        all_inputs["chart_type"] = drilldown_type
                        all_inputs["agg"] = "raw"
                        all_inputs["modal"] = True

                        data_id = get_data_id(pathname)
                        data = global_state.get_data(data_id)
                        all_inputs["x"] = drilldown_x
                        x_style = None
                        if map_type != "choropleth":
                            all_inputs["x"] = "lat_lon"
                            lat, lon = (all_inputs.get(p) for p in ["lat", "lon"])
                            data.loc[:, "lat_lon"] = (
                                data[lat].astype(str) + "|" + data[lon].astype(str)
                            )
                            x_style = dict(display="none")
                        all_inputs["y"] = [map_val]
                        chart, _, _ = build_chart(data_id, data=data, **all_inputs)
                        return chart, x_style
                else:
                    x_filter = click_point.get("x")
                    point_filter = {x_col: x_filter}
                    if frame_col:
                        point_filter[frame_col] = click_point.get("customdata")
                    if drilldown_type == "histogram":
                        hist_chart = build_histogram(
                            data_id, y_col, query, point_filter
                        )
                        return hist_chart, dict(display="none")
                    else:
                        x_query = build_group_inputs_filter(
                            global_state.get_data(data_id),
                            [point_filter],
                        )
                        if not query:
                            query = x_query
                        else:
                            query = "({}) and ({})".format(query, x_query)
                        all_inputs["query"] = query
                        all_inputs["chart_type"] = drilldown_type
                        all_inputs["agg"] = "raw"
                        all_inputs["modal"] = True
                        all_inputs["x"] = drilldown_x
                        chart, _, _ = build_chart(get_data_id(pathname), **all_inputs)
                        return chart, None
        return None, dict(display="none")
Esempio n. 3
0
def test_build_query():
    with ExitStack() as stack:
        settings = {"1": {"columnFilters": {"foo": {"query": "foo == 1"}}}}
        stack.enter_context(mock.patch("dtale.global_state.SETTINGS",
                                       settings))
        assert utils.build_query("1") == "foo == 1"