def on_data( _ts1, _ts2, _ts3, _ts4, pathname, inputs, chart_inputs, yaxis_data, map_data, last_chart_inputs, ): """ dash callback controlling the building of dash charts """ all_inputs = dict_merge(inputs, chart_inputs, dict(yaxis=yaxis_data or {}), map_data) if all_inputs == last_chart_inputs: raise PreventUpdate if is_app_root_defined(dash_app.server.config.get("APPLICATION_ROOT")): all_inputs["app_root"] = dash_app.server.config["APPLICATION_ROOT"] charts, range_data, code = build_chart(get_data_id(pathname), **all_inputs) return ( charts, all_inputs, range_data, "\n".join(make_list(code)), get_yaxis_type_tabs(make_list(inputs.get("y") or [])), )
def on_data(_ts1, _ts2, _ts3, pathname, inputs, chart_inputs, yaxis_data, last_chart_inputs): """ dash callback controlling the building of dash charts """ all_inputs = dict_merge(inputs, chart_inputs, dict(yaxis=yaxis_data or {})) if all_inputs == last_chart_inputs: raise PreventUpdate charts, range_data = build_chart(get_data_id(pathname), **all_inputs) return charts, all_inputs, range_data
def load_saved_chart(_ts, config, prev_config): if config == prev_config: raise PreventUpdate if config is None: return dict(display="none"), None, None, None if is_app_root_defined(dash_app.server.config.get("APPLICATION_ROOT")): config["app_root"] = dash_app.server.config["APPLICATION_ROOT"] charts, _, _ = build_chart(**config) return dict( display="block"), charts, config, build_saved_header(config)
def test_build_chart_type(): from dtale.dash_application.charts import build_chart import dtale.views as views with app.test_client() as c: with ExitStack() as stack: df, _ = views.format_data(pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6], c=[7, 8, 9]))) stack.enter_context(mock.patch('dtale.global_state.DATA', {c.port: df})) output = build_chart(c.port, chart_type='unknown', x='a', y='b') assert output[0].children[1].children == 'chart type: unknown'
def on_data(_ts1, _ts2, _ts3, _ts4, pathname, inputs, chart_inputs, yaxis_data, map_data, last_chart_inputs): """ dash callback controlling the building of dash charts """ all_inputs = dict_merge(inputs, chart_inputs, dict(yaxis=yaxis_data or {}), map_data) if all_inputs == last_chart_inputs: raise PreventUpdate charts, range_data, code = build_chart(get_data_id(pathname), **all_inputs) return charts, all_inputs, range_data, code, get_yaxis_type_tabs( make_list(inputs.get('y') or []))
def on_data( _ts1, _ts2, _ts3, _ts4, _ts5, _ts6, load, inputs, chart_inputs, yaxis_data, map_data, cs_data, treemap_data, last_chart_inputs, auto_load, prev_load_clicks, ): """ dash callback controlling the building of dash charts """ all_inputs = dict_merge( inputs, chart_inputs, dict(yaxis=yaxis_data or {}), map_data, cs_data, treemap_data, ) if not auto_load and load == prev_load_clicks: raise PreventUpdate if all_inputs == last_chart_inputs: raise PreventUpdate if is_app_root_defined(dash_app.server.config.get("APPLICATION_ROOT")): all_inputs["app_root"] = dash_app.server.config["APPLICATION_ROOT"] charts, range_data, code = build_chart(**all_inputs) return ( charts, all_inputs, range_data, "\n".join(make_list(code) + [CHART_EXPORT_CODE]), get_yaxis_type_tabs(make_list(inputs.get("y") or [])), load, dict(display="block" if valid_chart(**all_inputs) else "none"), )
def load_drilldown_content( _click_data_ts, drilldown_type, drilldown_x, inputs, chart_inputs, yaxis_data, map_data, click_data, drilldowns_on, ): if not drilldowns_on: raise PreventUpdate data_id = inputs["data_id"] 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: 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(**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 = 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=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(**all_inputs) return chart, None return None, dict(display="none")
def on_data( _ts1, _ts2, _ts3, _ts4, _ts5, _ts6, _ts7, _ts8, load_clicks, inputs, chart_inputs, yaxis_data, map_data, cs_data, treemap_data, funnel_data, last_chart_inputs, auto_load, prev_load_clicks, ext_aggs, ): """ dash callback controlling the building of dash charts """ all_inputs = dict_merge( inputs, chart_inputs, dict(yaxis=yaxis_data or {}), map_data, cs_data, treemap_data, funnel_data, dict(extended_aggregation=ext_aggs or []) if inputs.get("chart_type") not in NON_EXT_AGGREGATION else {}, ) if not auto_load and load_clicks == prev_load_clicks: raise PreventUpdate if all_inputs == last_chart_inputs: raise PreventUpdate if is_app_root_defined(dash_app.server.config.get("APPLICATION_ROOT")): all_inputs["app_root"] = dash_app.server.config["APPLICATION_ROOT"] charts, range_data, code = build_chart(**all_inputs) agg_disabled = len(ext_aggs) > 0 ext_agg_tt = text("ext_agg_desc") ext_agg_warning = show_style(agg_disabled) if agg_disabled: ext_agg_tt = html.Div([ html.Span(text("ext_agg_desc")), html.Br(), html.Ul([ html.Li( extended_aggregations.build_extended_agg_desc(ext_agg), className="mb-0", ) for ext_agg in ext_aggs ]), ]) final_cols = build_final_cols( make_list(inputs.get("y")), inputs.get("z"), inputs.get("agg"), ext_aggs if inputs.get("chart_type") not in NON_EXT_AGGREGATION else [], ) return ( charts, all_inputs, range_data, "\n".join(make_list(code) + [CHART_EXPORT_CODE]), get_yaxis_type_tabs(final_cols), load_clicks, dict(display="block" if valid_chart(**all_inputs) else "none"), agg_disabled, ext_agg_tt, ext_agg_warning, )
def popup_figure_content(pathname, search): """ dash callback for building chart for popup """ params = chart_url_params(search) return build_chart(get_data_id(pathname), **params)[0]