Hi,
I am new to dash and hit another roadblock today.
In my app, I want to give the user the possivility to add upon every click on “+” (see graphic below) additional dropdowns and inputs. However, I would like to calculate, based on the selection made under GK and the Amount, a running total of Amount * k(GK), where k is a constant which depends on GK-selection and is retrieved from a pd-Dataframe.
Any suggestions how I can implement this would be appreciated.
Thank you!
import dash
from dash import Dash, dcc, html, State, MATCH, ALL
from dash.dependencies import Input, Output
from dash import html
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
import numpy as np
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
df = pd.read_csv('Zemente_Stand_08_22.csv', delimiter=';', encoding='utf8')
df1 = pd.read_csv('Ausgangstoffe_08_22.csv', delimiter=';', encoding='utf8')
app.layout = html.Div([
dbc.Row(
dbc.Col(
html.H1("Transportbeton",
style={'font-size': '36px', 'textAlign': 'center', 'justyfy-content': 'center', 'text-decoration': 'underline', 'background':'#636363', 'color':'white'}),
),
style={'height':'60px', 'background':'#636363'}
),
dbc.Row([
dbc.Col([
html.Div([html.H5('Zement', style={"height": "30px", 'margin-top': '12px', 'margin-bottom':'12px', 'justyfy-content': 'center'}),
dcc.Dropdown(
df["Werk"].unique(),
placeholder='Werk',
id='Werksauswahl'
),
html.Div([
dcc.Dropdown(
df["Zement"],
placeholder='Zementtyp',
id='Zementauswahl'
),]),
], style={'text-align':'left', "width": "15%", 'margin-right':'25px'}),
html.Div([html.H5('Menge [kg]', style={"height": "30px", 'margin-top': '12px', 'margin-bottom':'12px'}),
dcc.Input(
id='Zementmenge_input',
type='number',
value=0,
placeholder='Menge [kg]',
style={'height':'35px'}
),]),
], style={'display':'flex', "width": "30%"}),
html.Div(id='Zement_gesamt_gwp', style={'color':'red'}),
]),
dbc.Row([
dbc.Col([
html.Div([html.H5('GK', style={"height": "10px", 'margin-top': '12px', 'margin-bottom':'2px', 'justyfy-content': 'center'}),
], style={'text-align':'left', "width": "15%", 'margin-right':'25px'}),
html.Div([html.H5('Amount [kg]', style={"height": "10px", 'margin-top': '12px', 'margin-bottom':'2px', 'justyfy-content': 'center'}),
]),
], style={'display':'flex', "width": "30%"})],
),
html.Div([
html.H6('', style={"height": "2px", 'margin-top': '12px', 'margin-bottom':'12px', 'justyfy-content': 'center'}),
html.Div(id='dropdown-container', children=[]),
html.Button("+", id="add-filter", n_clicks=0),
html.Div(id='GK_gesamt_gwp', style={'color':'red'}),
html.Div(id='dropdown-container-output')
]),
])
@app.callback(
dash.dependencies.Output('Zementauswahl', 'options'),
[dash.dependencies.Input('Werksauswahl', 'value')])
def set_cement_plant (selected_werk):
return df['Zement'][df['Werk'] == selected_werk]
@app.callback(
dash.dependencies.Output('Zement_gesamt_gwp', 'children'),
[dash.dependencies.Input('Werksauswahl', 'value'),
dash.dependencies.Input('Zementauswahl', 'value'),
dash.dependencies.Input('Zementmenge_input', 'value')])
def get_gwp_value (selected_werk, selected_cement, selected_amount):
gwp = df['GWP_netto'][(df['Werk']==selected_werk) & (df['Zement']==selected_cement)]
zement_gwp = gwp*selected_amount
zement_gwp = round(zement_gwp, 2)
return zement_gwp.astype(str) + str(' kg CO2/e')
@app.callback(
Output('dropdown-container', 'children'),
Input('add-filter', 'n_clicks'),
State('dropdown-container', 'children'))
def display_dropdowns(n_clicks, children):
new_dropdown = dbc.Row([
dbc.Col([
dcc.Dropdown(
df1['Prozess'].loc[0:3],
placeholder='GK-Typ',
id={
'type': 'GK-dropdown',
'index': n_clicks}, style={'text-align':'left', "width": "230px", 'margin-right':'25px'}),
dcc.Input(id='GK-Menge', type='number', value=0),
], style={'display':'flex', "width": "30%"})],
)
children.append(new_dropdown)
return children
if __name__ == '__main__':
app.run_server(debug=True)