Oh thanks for sharing the resource. Here it is:
import dash
from dash import Dash, dcc, html, Input, Output, ALL, Patch, callback, dash_table, clientside_callback, State
import requests
import json
# app = Dash(__name__)
dash.register_page(__name__, path='/multicallback-component')
# Page Layout for Different Filters and Dropdown Selections
def layout(**query_strings):
print("query_strings", query_strings)
qualitative_vars = None
quantitative_vars = None
relation = ["Count", "Sum", "Average", "Mean and Standard Error", 'percentage', "No Relation"]
if query_strings.get('prodclidsid') and query_strings.get('tid'):
response = // Hit an api to get metadata for dropdowns
if response.status_code == 200:
data = response.json()
qualitative_vars = data
quantitative_vars = data
if data:
layout = html.Div(
[
dcc.Input(value=f"{json.dumps(query_strings)}", id="input",
type="text",
placeholder="",
style={'marginRight': '10px', 'display': 'none'}),
dcc.Input(value=query_strings.get("viewname"), id="view-name",
type="text",
placeholder="",
style={'marginRight': '10px', 'display': 'none'}),
dcc.Input(value=quantitative_vars, id="quantitative_vars", type="text", placeholder="",
style={'marginRight': '10px', 'display': 'none'}),
dcc.Input(value=qualitative_vars, id="qualitative_vars", type="text", placeholder="",
style={'marginRight': '10px', 'display': 'none'}),
html.Button("Add Trace", id="add-trace-btn", n_clicks=0),
html.Div(id="dropdown-container-div", children=[]),
dcc.Dropdown(id="relation-dropdown", options=relation),
dcc.Graph(id="graph-component", figure={}),
]
)
return layout
clientside_callback(
"""
async function(rel, indep, dep, viewname, input_value) {
query_strings = JSON.parse(input_value)
const request_obj = // request object for api
if (viewname && rel) {
// hit an api to get the figure in JSON format...
const data = await response.json();
console.log("graph obj",JSON.parse(data["data"]));
return JSON.parse(data["data"]); // data contains the JSON from plotly.io.to_json()
}
}
""",
Output("graph-component", "figure"),
# Input("view-input", "value"),
Input("relation-dropdown", "value"),
Input({"type": "indep-filter-dropdown", "index": ALL}, "value"),
Input({"type": "dep-filter-dropdown", "index": ALL}, "value"),
State("view-name", "value"),
State("input", "value")
)
@callback(
Output("dropdown-container-div", "children"),
Input("add-trace-btn", "n_clicks"),
State("qualitative_vars", "value"),
State("quantitative_vars", "value"),
)
def display_dropdowns(n_clicks, qualitative_vars, quantitative_vars):
patched_children = Patch()
new_dropdown_1 = dcc.Dropdown(
options=qualitative_vars,
id={"type": "indep-filter-dropdown", "index": n_clicks},
)
new_dropdown_2 = dcc.Dropdown(
options=quantitative_vars,
id={"type": "dep-filter-dropdown", "index": n_clicks},
)
patched_children.append(new_dropdown_1)
patched_children.append(new_dropdown_2)
return patched_children