I have two inputs that check if a user exists in the db using a user name or a user id. If the user exists in the db, the data gets loaded, if not, a modal pops up and the app user is asked to either get data or not.
- ‘Yes’ triggers a collection and closes the modal.
- ‘No’ closes the modal without a collection.
import dash_bootstrap_components as dbc
from dash import html, dcc
import dash
from dash.dependencies import Output, Input, State
app = dash.Dash(external_stylesheets=[dbc.themes.SUPERHERO])
app_theme = {'background_color_main': '#0F2537'}
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
dbc.Label("SCREEN NAME", style={'margin-right':'5px'}),
dcc.Input(
id='screen_name',
type='text',
placeholder='',
debounce=True,
inputMode='latin',
autoFocus=True,
autoComplete='on',
disabled=False,
required=False,
),],width=2),
dbc.Col([
dbc.Label("USER ID",style={'margin-right':'5px'}),
dcc.Input(
id='user_id',
type='number',
placeholder='',
debounce=True,
# pattern=r"^[0-9]",
autoFocus=True,
minLength=1,
autoComplete='on',
disabled=False,
required=False,
),
], width=2)
],style={'margin-top': '15px',
}),
dbc.Modal([
dbc.ModalBody([
dbc.Label('The requested user was not found in the database.'),
dbc.Label("Would you like to download the user's data?" ),
],className='mr-1', style={'textAlign':'center'}),
dbc.ModalFooter([
dbc.Button('Yes', color="success", className="me-1", id='collect_user', style={'border-radius': '5px'},
n_clicks=0),
dbc.Button('No', color="danger", className="me-1", id='close_collect_user', style={'border-radius': '5px'},
n_clicks=0)
],style={'textAlign':'center'} )
],
id='user_collection_modal',
is_open=False,
centered=True,
backdrop=True,
keyboard=True,
fade=True,
)
], fluid=True)
@app.callback(
Output('user_collection_modal', 'is_open'),
[Input('screen_name', 'value'),
Input('user_id', 'value'),
Input('collect_user', 'n_clicks'),
Input('close_collect_user', 'n_clicks'),
],
[State('user_collection_modal', 'is_open')])
# State('user_update_model', 'is_open')]
def collection_mode(input_screen_name, input_user_id, collect_user, close_collect_user):
from load.pg_db_functions import get_user
# check if the user exists in the database
if input_screen_name:
user = get_user(screen_name=input_screen_name)
if len(user) == 1:
# if user exists and data is not from today ask if the data needs to be updated
return not 'is_open'
else:
return 'is_open'
elif input_user_id:
user = get_user(user_id=input_user_id)
if len(user) == 1:
# if user exists and data is not from today ask if the data needs to be updated
return not 'is_open'
else:
return 'is_open'
elif (collect_user==1) and (input_user_id):
from tw_procssing import tw_collect_user
tw_collect_user(user_id=input_user_id)
return not 'is_open'
elif (collect_user==1) and (input_screen_name):
from tw_procssing import tw_collect_user
tw_collect_user(user_id=input_screen_name)
return not 'is_open'
elif (close_collect_user==1):
return not 'is_open'
app.run_server(debug=True, port=8090, use_reloader=False)
I keep getting an error
Traceback (most recent call last):
File "D:\ProgramData\Anaconda3\Lib\site-packages\dash\_callback.py", line 151, in add_context
output_value = func(*func_args, **func_kwargs) # %% callback invoked %%
TypeError: collection_mode() takes 4 positional arguments but 5 were given