S
Solara•2mo ago
Punxsutawney

Authorization example using a Thread/Task is even possible?

Hello! I'm playing with/studying the Authorization https://solara.dev/apps/authorization/users example. I'm trying to extend it to show a Spinner / ProgressLinear once the user clicks the Login button because in my use case the login function could take several seconds to finish. I would like to hide/change the Login button for a spinner. I have tried several task related methods, trying to make the login function a task, called from LoginForm. But I'm not able to get it properly working, sometimes the spinner is not shown, but the function success (due to user and login_failed hooks sometimes) others it shows it but the app goes crazy. I think it could be related to how Layout and check_auth are being magically managed behind the scenes. I don't know. At this point, I'm about to throw the towel with this example and try another approach 😕 but before I would like to know if there is an "easy" way to solve this for this example or if this is a deadend and I should indeed take another aproach. Thanks!
5 Replies
MaartenBreddels
MaartenBreddels•2mo ago
I hope this demo helps: https://py.cafe/maartenbreddels/solara-auth-example Does this work for you? If yes, can you tell us what you did wrong so we can improve the documentation?
PyCafe - Solara - Authorization and authentication example - Solara
Create & Share Streamlit, Dash and Python Apps Online.
Punxsutawney
PunxsutawneyOP•2mo ago
It works now!. Thanks a lot! I see what is different, not sure I understand why it matters so much.
My code differs in that I changed the lambda call to the the global @task decorated task function for a local @task decorated function call because I wanted to do more things locally using the local context... (I'm not using async/coroutines in my code but your approach works now for my code also) .. This was my NOT working code, the rest is like the original: @solara.component def LoginForm(): username = solara.use_reactive("admin") password = solara.use_reactive("admin") @solara.lab.task def check_credentials(): print('check_credentials - enter') return login(username.value, password.value) with solara.Card("Login"): solara.Markdown( """ This is an example login form.
use admin/admin to login as admin. use test/test to login as a normal user.""") solara.InputText(label="Username", value=username) solara.InputText(label="Password", password=True, value=password) solara.Button(label="Login", on_click=check_credentials, disabled=check_credentials.pending) solara.ProgressLinear(check_credentials.pending) if check_credentials.value: solara.Error("Wrong username or password")
MaartenBreddels
MaartenBreddels•2mo ago
you keep re-defining the task, because it runs in the body of the component, for this, you should be using use_task
Punxsutawney
PunxsutawneyOP•2mo ago
I see, of course, I do re-redefine it!, thanks for pointing it out. The funny thing is that I use task for other activities and so far everything works pretty well. I even tried some other approaches here using use_task also but I had trouble to activate the task just using the button click, never did this before, I tried making it dependant on a use_reactive variable than was set by the button, but things got a bit messy very quickly and it was not working well....
MaartenBreddels
MaartenBreddels•2mo ago
did you make it depend on the reactive variable object, or its value? the value can change, while the reactive variable does not could that be it?
Want results from more Discord servers?
Add your server