Hello,
I would like to use this workaround with a picklist suggesting some initial default values.
First issue is that the 1st time the page is loaded, the list shows the X items I want to suggest, but the Bar graph doesnt display anything until i pick a X+1th item in the list. I tried to change a bit the logic in the code but without success.
Additionally, I aim to add either a button or another Checklist to reset the list to the default suggestions, but prior to this I have to fix the first issue.
It makes sense because the graph presents the sales of ~50 products, but 10 out of 50 represents 95% of the sales. So, by default, I want to show the sales figures for these 10 products.
Below a code illustrating the current the problem, with the workaround presented above.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_daq as daq
from dash.dependencies import Input, Output, State
import plotly.graph_objs as go
from dash.exceptions import PreventUpdate
xTime=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
listofY=['y1','y2','y3','y4','y5','y6','y7','y8','y9','y10']
Table={
'y1':[20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17],
'y2':[19, 18, 22, 14, 15, 19, 15, 14, 14, 21, 16, 16],
'y3':[11, 14, 21, 16, 16, 10, 15, 11, 10, 12, 11, 16],
'y4':[14, 10, 27, 14, 16, 14, 15, 18, 14, 12, 12, 16],
'y5':[16, 15, 22, 18, 18, 19, 15, 13, 15, 12, 18, 17],
'y6':[18, 17, 25, 14, 16, 11, 13, 14, 20, 14, 12, 18],
'y7':[11, 14, 22, 14, 13, 19, 15, 17, 14, 12, 17, 19],
'y8':[13, 14, 20, 14, 16, 17, 15, 14, 10, 13, 14, 20],
'y9':[15, 15, 22, 15, 11, 13, 15, 14, 11, 14, 12, 22],
'y10':[10, 11, 22, 14, 16, 21, 15, 14, 10, 12, 11, 16]
}
app = dash.Dash()
app.layout = html.Div([
html.Div([
dcc.Dropdown(
id='product-choice',
options=[{'label': i, 'value': i} for i in listofY],
value=['y1', 'y2', 'y5'],
multi=True
),
html.Div([
dcc.Checklist(
id='select-all',
options=[{'label': 'Select All', 'value': 1}],
values=[]
)
], id='checklist-container')
]),
html.Div(children=[
dcc.Graph(
id='bar-graph-by-model'
),
]),
])
# Below the Workaround suggested by mikesmith1611
#@app.callback(
# Output('product-choice', 'value'),
# [Input('select-all', 'values')],
# [State('product-choice', 'options')])
#def test(selectALL, options):
# if len(selectALL) > 0:
# return [i['value'] for i in options]
# else:
# raise PreventUpdate()
#
#@app.callback(
# Output('checklist-container', 'children'),
# [Input('product-choice', 'value')],
# [State('product-choice', 'options'),
# State('select-all', 'values')])
#def tester(chosenValues, availableChoices, selectALL):
#
# if len(chosenValues) < len(availableChoices) and len(selectALL) == 0:
# raise PreventUpdate()
#
# elif len(chosenValues) < len(availableChoices) and len(selectALL) == 1:
# return dcc.Checklist(id='select-all',
# options=[{'label': 'Select All', 'value': 1}], values=[])
#
# elif len(chosenValues) == len(availableChoices) and len(selectALL) == 1:
# raise PreventUpdate()
#
# return dcc.Checklist(id='select-all',
# options=[{'label': 'Select All', 'value': 1}], values=[1])
#
@app.callback(
Output('bar-graph-by-model', 'figure'),
[Input('product-choice', 'value')]
)
def updategraph(value):
traces=[]
for i in value:
x=xTime
ytemp=Table[i]
traces.append(go.Bar(
x=x,
y=ytemp,
name=i,
))
return {
'data': traces,
'layout': go.Layout(
)
}
if __name__ == '__main__':
app.run_server(debug=True, port=8051)
If the list shows the suggested values when the page is loaded, why does the graph stays empty ?