What I’m trying to do:
I want to create a global variable and reuse it in my application on different forms. I know about module, but form/app structure is kind of different and either I am not doing it right or it just wont work.
The variable i am trying to use is google email used to login.
users_email = anvil.users.login_with_google()
self.users_email.text = users_email['email']
print(self.users_email.text)
user = app_tables.onboard.get(email=self.users_email.text)
if user is None:
# If the user does not exist, open the New User Page
open_form('Login.New_User_Page')
else:
# User exists, so check their type of business
type_user = user['type_of_business']
if type_user == "Admin":
open_form('Login.Admin_page')
elif type_user == "Bodyshop":
open_form('Login.Body_Shop_Page')
elif type_user == "Independent":
open_form('Login.Independent_page')
What I’ve tried and what’s not working:
Clone link:
share a copy of your app
It isn’t clear at all what variable you’re trying to use as a global variable, since there is no module to hold any global variables. You say you want users_email
to be global, but it is only set in the one spot.
The general approach is to create a client module at the top level, e.g. Globals
, and then import it and use it to set variables on. e.g.:
# Globals module
user_email = None
Then in a form that should use it.
# Use the autocomplete to get the number of dots right, but use this format
from . import Globals
Globals.user_email = whatever
Thank you for your quick response @jshaffstall . Where would i place the Globals module in my structure to be able to access it in all forms?

At the top level. Click the three dots next to Client Code and create a module.
My question is that its in the middle of all my of my forms. Does it need to be nested into a specific location to be able to be used throughout my entire app? As you can see the structure is a bit different.

It can honestly be anywhere, but for ease of use create it at the top level. Click the three dots next to Client Code, not next to Login.

3 Likes
Awesome it worked. But…
I have to use
from .. import Globals
Why is it two dots instead of one? Curious
Thank you for your help.
@jshaffstall
The number of dots in the import depends on the relative nesting of the module versus everything else. The autocomplete will always get that right.
If you’re familiar with .
and ..
in file systems, they serve the same purpose in Python imports.
1 Like
And for most Forms, Anvil’s navigation pane hides one level of nesting. The source code file of the Form is actually one level deeper than it appears.
1 Like