Hi Emil!
Thanks for making an amazing extension for Dash users!
I would like to ask why does my code not work with MultiplexerTransform()
This is the example:
from dash import Dash, Input, Output, State, html, dcc, dash_table, callback
import os
import pandas as pd
import dash_bootstrap_components as dbc
from dash_extensions.enrich import DashProxy, MultiplexerTransform, Input, Output, State
from dash.exceptions import PreventUpdate
# does importing input output state from dash_extensions automatically replace the input output and state of Dash library?
PATH = os.getcwd()
PATH_DATA = PATH + "/datasets/"
df_contract = pd.read_excel(PATH_DATA + "Faker Contrat.xlsx")
app = DashProxy(
prevent_initial_callbacks=True,
suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.BOOTSTRAP],
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
transforms=[MultiplexerTransform()]
)
buttongroup = dbc.ButtonGroup(
[
dbc.Button('Save', color='primary', className="mr-1", id="save"),
dbc.Button('Refresh', color='warning', className="mr-1", id="refresh"),
dbc.Button('Apply', color='success', className="", id="apply")
],
id="buttongroup",
size='md',
className="d-flex position-relative",
)
app.layout = dbc.Container(
dbc.Card(
[
dbc.CardHeader(
[
html.H3("Heading"),
]
),
dbc.CardBody(
[
html.H4("Insurers"),
dcc.Dropdown(
options=[
{'label': name, "value": name} for name in df_contract['Assureur'].drop_duplicates()
],
value=[],
id="dropdown-1",
multi=True,
),
dcc.Checklist(
options=[{"label": "All", "value": "All", "disabled": False}],
value=[],
id="checklist-1"
),
html.Hr(),
html.H4("Life Insurance Plans"),
dcc.Dropdown(
options=[
{'label': name, "value": name} for name in df_contract['Nom Assurance Vie'].drop_duplicates()
],
value=[],
id="dropdown-2",
multi=True,
),
dcc.Checklist(
options=[{"label": "All", "value": "All", "disabled": False}],
value=[],
id="checklist-2"
),
html.Hr(),
html.Div(id='table')
]
),
dbc.CardFooter(
[
buttongroup
]
)
],
body=True,
className="h-100 flex-column",
),
style={"height": "100vh"},
)
@callback(
Output("dropdown-1", "value"),
Input("checklist-1", "value")
)
def select_all(value):
if value == ["All"]:
return df_contract['Assureur'].drop_duplicates()
@callback(
Output("dropdown-2", "options"),
Input("dropdown-1", "value"),
)
def asd(value):
if value is None:
raise PreventUpdate
else:
df_temp = df_contract[df_contract['Assureur'].isin(value)]
return [{'label': name, "value": name} for name in df_temp['Nom Assurance Vie']]
@callback(
Output("table", "children"),
Input("apply", "n_clicks"),
State("dropdown-1", "value"),
State("dropdown-2", "value")
)
def update_table(n, val1, val2):
if n is None:
raise PreventUpdate
else:
filter_insurer = df_contract["Assureur"].isin(val1)
filter_plans = df_contract["Nom Assurance Vie"].isin(val2)
df_0 = df_contract[filter_insurer]
df_1 = df_0[filter_plans]
return dash_table.DataTable(
columns=[{"name": i, "id": i} for i in df_contract.columns],
data=df_1.to_dict('records'),
style_cell={"font-family": "sans-serif"},
fixed_columns={'headers': True, 'data': 2},
style_table={'minWidth': '100%'},
style_as_list_view=True
)
@callback(
output=dict(
dd1=Output("dropdown-1", "value"),
dd2=Output("dropdown-2", "value")
),
inputs=dict(
refresh=Input("refresh", "n_clicks"),
),
)
def contract_refresh(refresh):
if refresh is None:
raise PreventUpdate
if refresh > 0:
return dict(
dd1=[],
dd2=[]
)
if __name__ == "__main__":
app.run_server(port=1111, debug=True)
everything works until I put @callback def contract_refresh
(the very last one) and it shows this error:
The sample data used looks like this:
ID |
Assureur |
Nom Assurance Vie |
Frais UC (%) |
Frais SCPI (%) |
Frais PEP (%) |
Frais sur versement (%) |
Frais dāadministration (%) |
Frais fonds en euros (%) |
Frais arbitrage FE et UC (%) |
Frais arbitrege SCPI (%) |
Souscription minimum |
Versement minimum |
Droits dāentrĆ©e |
UNPMSP |
UNEP |
Unep MultisƩlection Plus |
1,00 |
1,14 |
1,00 |
5,00 |
0,00 |
1,00 |
0,00 |
0,50 |
1 200,00 |
1 200,00 |
11,00 |
UNPEVL |
UNEP |
Unep Evolution |
0,90 |
1,00 |
0,00 |
3,00 |
0,00 |
0,98 |
0,50 |
0,50 |
50 000,00 |
|
|
AXAEXL |
AXA |
Axa Excelium |
0,96 |
0,90 |
0,00 |
4,85 |
0,00 |
0,80 |
0,00 |
0,00 |
750,00 |
750,00 |
750,00 |
AXAMIL |
AXA |
Axa Millenium |
0,96 |
1,00 |
0,00 |
2,00 |
0,00 |
0,90 |
0,40 |
0,50 |
- |
- |
- |
The app looks like this:
Thank you so much in advance!