"Global" variables, scoped to user

It seems that there are two scopes for reactive variables: global, or app-level variables that seem to be shared by all users on a server; and local, component-level variables that are contained within a defined solara component. However, it's not clear how to create variables that are shared across components but are still scoped to an individual user. Currently, we have to maintain an accessible dictionary that contains the session id and individual instances of global variables in order to share across pages/component without affecting other users. Is there a more appropriate way to handle this use case?
7 Replies
MaartenBreddels
MaartenBreddels5mo ago
Hi Nick, there is only 1 scope for reactive variables, and it is per kernel (=per page/user, ignoring ipypopout): https://solara.dev/documentation/api/utilities/reactive What made you think reactive variables were not scoped? And any advice on how to improve the docs on that?
animusater
animusater5mo ago
Hmm, that's not what we're seeing. In a solara app hosted on AWS, using solara-auth to manage logged-in users, if a reactive variable is defined outside a Page (that is, at the top-level e.g. some_var = solara.reactive(0)), that value is changed for every user currently logged in and using the app.
MaartenBreddels
MaartenBreddels5mo ago
do you reassign (some_var.value = ...) or do you mutate (some_var.value.append(...) ?
animusater
animusater5mo ago
We reassign using set: some_value.set(...).
MaartenBreddels
MaartenBreddels5mo ago
If you take the quickstart example at https://solara.dev/documentation/getting_started and open two tabs, you should see both 'tabs' live in their own scope. I wonder what is different in your situation.
Solara Quickstart
Get started with building web- and data-apps in pure python with Solara.
animusater
animusater5mo ago
Hmm, what variables in general would be scoped to a specific user? Only those defined on a single page? What about an app state specific to the user, something that doesn't live on a single page but is reference/imported by several? I see that in the auth handling, the Reactive variable is passed a key pointing to solara-enterprise.auth.user, is this the way to have a globally accessible variable that's scoped to a local user? Or, in general, can we assume that anything instances within the pages directory is local to the browser, while things outside are app-level global?
MaartenBreddels
MaartenBreddels5mo ago
Only reactive variables are scoped, similar to thread local storage. For the rest we don’t do any magic, we just use regular Python scopes. So if you have a dataframe created top level in a module, everybody shares the same dataframe.
Want results from more Discord servers?
Add your server