-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add loading spinner on login #172
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,11 @@ | |
Output("user_credentials", "data"), | ||
Output("user_headers", "data"), | ||
Output("form_feedback_area", "children"), | ||
Output("username_input", "style"), | ||
Output("password_input", "style"), | ||
Output("send_form_button", "style"), | ||
Output("form_feedback_area", "style"), | ||
Output("loading_spinner", "style"), | ||
], | ||
Input("send_form_button", "n_clicks"), | ||
[ | ||
|
@@ -52,12 +57,28 @@ def login_callback(n_clicks, username, password, user_headers): | |
This function is triggered when the login button is clicked. It verifies the provided username and password, | ||
attempts to authenticate the user via the API, and updates the user credentials and headers. | ||
If authentication fails or credentials are missing, it provides appropriate feedback. | ||
After login succeeds and while data required to boot the dashboard is being fetched from the API, | ||
the login form is hidden and a spinner is displayed. | ||
|
||
Returns: | ||
dash.dependencies.Output: Updated user credentials and headers, and form feedback. | ||
dash.dependencies.Output: Updated user credentials and headers, and form feedback + styles to hide/show login elements and loading spinners. | ||
""" | ||
input_style_unchanged = {"width": "250px"} | ||
empty_style_unchanged = {"": ""} | ||
hide_element_style = {"display": "none"} | ||
show_spinner_style = {"transform": "scale(4)"} | ||
|
||
if user_headers is not None: | ||
return dash.no_update, dash.no_update, dash.no_update | ||
return ( | ||
dash.no_update, | ||
dash.no_update, | ||
dash.no_update, | ||
input_style_unchanged, | ||
input_style_unchanged, | ||
empty_style_unchanged, | ||
empty_style_unchanged, | ||
hide_element_style, | ||
) | ||
|
||
if n_clicks: | ||
# We instantiate the form feedback output | ||
|
@@ -70,7 +91,16 @@ def login_callback(n_clicks, username, password, user_headers): | |
form_feedback.append(html.P("Il semble qu'il manque votre nom d'utilisateur et/ou votre mot de passe.")) | ||
|
||
# The login modal remains open; other outputs are updated with arbitrary values | ||
return dash.no_update, dash.no_update, form_feedback | ||
return ( | ||
dash.no_update, | ||
dash.no_update, | ||
form_feedback, | ||
input_style_unchanged, | ||
input_style_unchanged, | ||
empty_style_unchanged, | ||
empty_style_unchanged, | ||
hide_element_style, | ||
) | ||
else: | ||
# This is the route of the API that we are going to use for the credential check | ||
try: | ||
|
@@ -80,12 +110,26 @@ def login_callback(n_clicks, username, password, user_headers): | |
{"username": username, "password": password}, | ||
client.headers, | ||
dash.no_update, | ||
hide_element_style, | ||
hide_element_style, | ||
hide_element_style, | ||
hide_element_style, | ||
show_spinner_style, | ||
) | ||
except Exception: | ||
# This if statement is verified if credentials are invalid | ||
form_feedback.append(html.P("Nom d'utilisateur et/ou mot de passe erroné.")) | ||
|
||
return dash.no_update, dash.no_update, form_feedback | ||
return ( | ||
dash.no_update, | ||
dash.no_update, | ||
form_feedback, | ||
input_style_unchanged, | ||
input_style_unchanged, | ||
empty_style_unchanged, | ||
empty_style_unchanged, | ||
hide_element_style, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MateoLostanlen @fe51 @RonanMorgan Another example where
Maybe there are workarounds to fix that in Dash but this kind of update behavior should really come out of the box. |
||
|
||
raise PreventUpdate | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cleaner to user
{}
instead, but thenmypy
complains because it wants to know the type ofempty_style_unchanged
and it seems python does not infer types for empty dictionaries => using{"": ""}
seems simpler than bringing in a whole typing library to handle that error only (from typing import Dict, Optional ... empty_style: Dict[str, Optional[str]] = {}
).