In my app, I have an option two create a new project, when a button is clicked its reads the state of the input value. Then a new project is created in the database. The user is also updated if the request was successful or not. The part I am struggling with is updating the dropdown, containing all the projects. Because a callback function can only output one component. I have to choose, either show the user creating the project was successful or updating the dropdown. I did try to create a second callback function to update the dropdown. This drop-down is than only updated on a page reload…
@app.callback(Output('project_add_status', 'children'), [Input('create_project_button', 'n_clicks')], [State('input_box_project_name', 'value')])
def create_project(n_clicks, value):
if value:
try:
with database.atomic():
# Attempt to create a new project, If the project name is already taken, due the
# unique constraint, the database will raise an IntegrityError
project = Project.create(name=value)
for project in Project.select():
print(project.name)
return 'Project ' + value + ' is added'
except IntegrityError:
return 'Project name is already taken'
else:
pass
@app.callback(Output('project_drop_down', 'options'), [Input('project_drop_down', 'value')])
def update_drop_down_project(value):
# Safely reassign the filter to a new variable
print('test')
values = {}
for project in Project.select():
values.update({'label': project.name, 'value': project.name})
return [values]
Maybe the above does not make any sense, so this is what I want to do:
- When the button is clicked, update the database with the new project.
- Show the user if this was successful or not
- Update the dropdown