For the below code update and clear button are not working if i select any course other than Initial state
I have been trying to figure out a solution for almost a week cananyone help me with this.
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_auth
import pandas as pd
VALID_USERNAME_PASSWORD_PAIRS = {
'admin': 'password'}
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
auth = dash_auth.BasicAuth(
app,
VALID_USERNAME_PASSWORD_PAIRS)
tasks = pd.read_csv('tasks.csv')
courses = tasks['Course'].unique()
selected_tasks = tasks[tasks['Course'] == courses[0]]
course = courses[0]
app.layout = html.Div([
html.H1('Task Management System', className='text-center mt-5'),
html.Div([
dcc.Dropdown(
id='course-dropdown',
options=[{'label': course, 'value': course} for course in courses],
value=courses[0]
)
], className='mb-3'),
html.Div(id='table-container'),
dbc.Button('Update Progress', id='update-button', className="btn btn-success float-start", n_clicks=None),
dbc.Button('Clear Progress', id='clear-button', className="btn btn-danger float-end",n_clicks=None)
], className='container')
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('course-dropdown', 'value')]
)
def display_tasks(tmp):
global course
global selected_tasks
course = tmp
selected_tasks = tasks[tasks['Course'] == course]
return dbc.Table([
html.Thead([html.Tr([html.Th('Task'), html.Th('Completion')])]),
html.Tbody([
html.Tr([html.Td(task),
html.Td(dcc.Slider(id=f"{course}-{task}", value=value, min=0, max=3, step=1, marks={0:'yet to start', 1:'completed learning', 2:'completed practice', 3:'completed tests'}))])
for task, value in zip(selected_tasks['Task'], selected_tasks['Completion'])
])
], className='mt-3 mb-3')
@app.callback(
[dash.dependencies.Output(f"{course}-{task}", 'value') for course, task in zip(selected_tasks['Course'], selected_tasks['Task'])],
[dash.dependencies.Input('course-dropdown', 'value'),
dash.dependencies.Input('update-button', 'n_clicks'),
dash.dependencies.Input('clear-button', 'n_clicks')],
[dash.dependencies.State(f"{course}-{task}", 'value') for course, task in zip(selected_tasks['Course'], selected_tasks['Task'])]
)
def update_tasks(value, update_clicks, clear_clicks, *values):
global selected_tasks
global tasks
global course
if value:
if update_clicks is not None:
selected_tasks['Completion'] = values
tasks.update(selected_tasks)
tasks.to_csv('tasks.csv', index=False)
elif clear_clicks is not None:
tasks = pd.read_csv('tasks.csv')
selected_tasks = tasks[tasks['Course'] == course]
return [selected_tasks.at[i, 'Completion'] for i in range(len(selected_tasks))]
if __name__ == '__main__':
app.run_server(debug=True)
This is task.csv
Course,Task,Completion
C01,C01T01,2
C01,C01T02,3
C02,C02T01,0
C02,C02T02,3
C02,C02T03,3