I have pattern matching ids, and the callbacks are not firing when Loading the app
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State, MATCH, ALL
import pandas as pd
from datetime import date
import datetime
import calendar
import random
from dash.exceptions import PreventUpdate
external_stylesheets = [dbc.themes.BOOTSTRAP]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, )
def idFunction(ind, indexList, i, days, weekDay):
if ind==0 and days[weekDay][ind] not in [1,2,3,4,5,6]:
m_id=date(2021, i-1, days[weekDay][ind])
elif ind==indexList[-1] and days[weekDay][ind] not in [24,25,26,27,28,29, 30,31]:
m_id=date(2021, i+1, days[weekDay][ind])
else:
m_id=date(2021, i, days[weekDay][ind])
return m_id
my_calendar = html.Div([
dbc.Row(
dbc.Col(
dcc.Dropdown(
id="month",
options=[{
"label": x,
"value": x
} for x in calendar.month_name],
value=calendar.month_name[date.today().month],
clearable=False,
))),
html.Br(),
#dbc.Row(html.P(id="dbc-button")),
])
table_header = [
html.Thead(html.Tr([html.Th("KEY"), html.Th(dbc.Badge("RECOVERY", color="primary")), html.Th(dbc.Badge("PREPARATIVE", color="success")), html.Th(dbc.Badge("ORGANIZATIONAL", color="warning")), html.Th(dbc.Badge("MAINTENANCE", color="info")), html.Th(dbc.Badge("THRESOLD", color="light")), html.Th(dbc.Badge("FITNESS", color="dark")), html.Th(dbc.Badge("GAME", color="danger")) ]))
]
row1 = html.Tr([html.Td("RPE"), html.Td("<3"),html.Td("3-4"), html.Td("4-5"), html.Td("5-6"), html.Td("7-8"), html.Td("8-9"), html.Td("9-10")])
row2 = html.Tr([html.Td("VOL"), html.Td("<35min"), html.Td("35-45min"), html.Td("45-60min"), html.Td("45-75min"), html.Td("75-80min"), html.Td("80-100min"), html.Td("90min")])
row3 = html.Tr([html.Td("TL"), html.Td("<100"), html.Td("105-200"), html.Td("180-325"), html.Td("300-450"), html.Td("490-640"), html.Td("640-900"), html.Td(">810")])
row4 = html.Tr([html.Td("Readiness"), html.Td("<65"), html.Td(">77.5"), html.Td("66-75"), html.Td("70-80"), html.Td("75-85"), html.Td("82.5-95"), html.Td("87.5")])
row5 = html.Tr([html.Td("Recovery"), html.Td("n/a"), html.Td("12hr"), html.Td("<24hr"), html.Td("24hr"), html.Td("48hr"), html.Td("72hr"), html.Td(">72hr")])
table_body = [html.Tbody([row1, row2, row3, row4, row5])]
table = dbc.Table(table_header + table_body, bordered=True,
hover=True,
responsive=True,
striped=True,)
app.layout = html.Div([
html.Div(my_calendar, id='monthChoice'),
dbc.Row([
dbc.Col(
[html.Div("Monday"),
html.Div(id='monday')]),
dbc.Col([html.Div("Tuesday"),
html.Div(id='tuesday')]),
dbc.Col([html.Div("Wednesday"),
html.Div(id='wednesday')]),
dbc.Col([html.Div("Thursday"),
html.Div(id='thursday')]),
dbc.Col([html.Div("Friday"),
html.Div(id='friday')]),
dbc.Col([html.Div("Saturday"),
html.Div(id='saturday')]),
dbc.Col([html.Div("Sunday"),
html.Div(id='sunday')])
], no_gutters=True),
html.Div(table),
html.Div(id="output"),
dbc.Row(html.Div(id='Training-level')),
])
@app.callback([Output("monday", "children"),Output("tuesday", "children"), Output("wednesday", "children"), Output("thursday", "children"), Output("friday", "children"), Output("saturday", "children"), Output("sunday", "children"), Output("output", "children") ], [Input("month", "value")])
def table(value):
df = [
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
]
data1 = [
'Preparative', 'Organizational', 'Maintenance', 'Thresold', 'Fitness',
'Game', 'Recovery'
]
print((value))
for i, month in enumerate(df):
if month == value:
break
i = i + 1
days = calendar.monthcalendar(2021, i)
previousMonth=calendar.monthcalendar(2021, i-1)
nextMonth=calendar.monthcalendar(2021, i+1)
a=0
for j in days[0]:
if j ==0:
days[0][a]=previousMonth[-1][a]
a+=1
a=0
for j in days[-1]:
if j ==0:
days[-1][a]=nextMonth[0][a]
a+=1
monday = []
tuesday=[]
wednesday=[]
thursday=[]
friday=[]
saturday=[]
sunday=[]
data1 = [
'Preparative', 'Organizational', 'Maintenance', 'Thresold', 'Fitness',
'Game', 'Recovery', 'OFF'
]
j = days
days = pd.DataFrame(days)
days.columns = [
'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
'Sunday'
]
for ind in days.index:
m_id=str(idFunction(ind, days.index, i, days, 'Monday'))
t_id=str(idFunction(ind, days.index, i, days, 'Tuesday'))
w_id=str(idFunction(ind, days.index, i, days, 'Wednesday'))
th_id=str(idFunction(ind, days.index, i, days, 'Thursday'))
f_id=str(idFunction(ind, days.index, i, days, 'Friday'))
s_id=str(idFunction(ind, days.index, i, days, 'Saturday'))
su_id=str(idFunction(ind, days.index, i, days, 'Sunday'))
print("Here")
if days['Monday'][ind] != 0:
monday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Monday'][ind],
id={
'type': 'date-button',
'index': m_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index': m_id}),
])))
if days['Tuesday'][ind] != 0:
tuesday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Tuesday'][ind],
id={
'type': 'date-button',
'index': t_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index': t_id})
])))
if days['Wednesday'][ind] != 0:
wednesday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Wednesday'][ind],
id={
'type': 'date-button',
'index': w_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index': w_id})
])))
if days['Thursday'][ind] != 0:
thursday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Thursday'][ind],
id={
'type': 'date-button',
'index': th_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index': th_id})
])))
if days['Friday'][ind] != 0:
friday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Friday'][ind],
id={
'type': 'date-button',
'index': f_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index':f_id})
])))
if days['Saturday'][ind] != 0:
saturday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Saturday'][ind],
id={
'type': 'date-button',
'index': s_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index': s_id})
])))
if days['Sunday'][ind] != 0:
sunday.append(
dbc.Card(
dbc.CardBody([
html.Div(
dbc.Button(children=days['Sunday'][ind],
id={
'type': 'date-button',
'index': su_id
},
color="link")),
html.Div(id={
'type': 'dropdown',
'index': su_id})
])))
trap=html.Div("Hello")
return monday,tuesday, wednesday, thursday, friday, saturday, sunday, trap
@app.callback(
Output({
'type': 'dropdown',
'index': MATCH}, 'children'),
Input({
'type': 'date-button',
'index': MATCH}, 'id'))
def update(id):
print(id)
value=id['index']
data1 = [
'Preparative', 'Organizational', 'Maintenance', 'Thresold', 'Fitness',
'Game', 'Recovery', 'OFF'
]
return dcc.Dropdown(
id={
'type': 'dropdown2',
'index': value },
options=[{
"label": x,
"value": x
} for x in data1],
value=data1[7],
clearable=False,
)
@app.callback(
[dash.dependencies.Input({
'type': 'dropdown2',
'index': MATCH }, 'value')])
def update_output(value):
print('You have selected "{}"'.format(value))
if __name__ == '__main__':
app.run_server(host='0.0.0.0', port=random.randint(2000, 9000), debug=False)
Can soneone explain to me what the issue is ?