NameError: name 'variable' is not defined (even though it is)

Hello. I am working on an App. I am sending inputs from the user to the ServerModule and assigning that value to other variables. And later, i am calling a function “math” that uses those variables to perform specific equations. But while doing this, in getting a NameError. Here is a code:

import anvil.server
@anvil.server.callable()
def initial_i(infected):
  iI=infected
@anvil.server.callable()
def suceptible(sus):
  S=sus
@anvil.server.callable()
def contact(con):
  icontact=con
@anvil.server.callable()
def infp(infp):
  duration=infp
@anvil.server.callable()
def contact_new(new):
  contact=new
@anvil.server.callable()

def math():
  
  infection_rate=3
  B1=icontact*infection_rate
  R01=B1*duration
  if R01<1:
    R01_outcome="Decline"
  elif R01==1:
    R01_outcome="Steady"
  else:
    R01_outcome="Exponential"
  I_normal=B1*iI*S
  B=contact*infection_rate
  R0=B*duration
  if R0<1:
    R0_outcome="Decline"
  elif R0==1:
    R0_outcome="Steady"
  else:
    R0_outcome="Exponential"
  I=B*iI*S
  change_i=I/I_normal*100

  return R01,R01_outcome,R0,R0_outcome,change_i

And the Error:

NameError: name 'icontact' is not defined
at ServerModule1, line 22

I found that is is showing only the ‘icontact’ variable is error as it is the first one mentioned in the “math” function. So, all the other variables are also not declared (Why?).So, can anyone please tell me how to fix this Error…Thank You

On the face of it, looking at the code you’ve pasted here, that’s a scope thing.

You are defining the variable inside a function. Once that function completes, the variable is no longer visible.

I would do something like this :

# In your form ...
icontact = anvil.server.call("contact", con)
infection_rate =  ... etc.
...
result = anvil.server.call("math", icontact, infection_rate, ... etc)
1 Like

Ok Thank You. Let me try it and i will get back to you

Also, if the functions are doing no more than just those simple assignments, I would just send the user inputs to the math function directly. That would save a lot of server calls.

1 Like

Oh! Ok. Let me do that

Hello David, actually I am calling the math function from an other form. So, to access that variables, i just need to type “from Form1 import Form1”, in the top right?
Edit: But im getting the NameError again. I think it is because I got the “from Form1 import Form1” syntax, wrong.

If you are swapping forms then you have the same issue in that when Form1 gets destroyed you lose the variables.

Create a module and call it “global” or something like that.
Define the variables in that module, like this :

contact = None # default value
... etc

Include that module in both forms.
Then from either form you can say something like :

globals.contact = "whatever"

to set the value, then :

anvil.server.call("math", globals.contact, ...)

to pass them to the function.

(note that is untested but should work)

1 Like

Okay. I will try it out

Im getting this Error:

NameError: name 'Global' is not defined

I made a module called “Global” and inside, typed:

initial_i=None
suceptible=None
contact=None
infp=None
contact_new=None

And when the values were inputted, i typed:

Global.contact=contact

In this case, it is contact. And in the from from which i am calling the math function, i typed:

R01,R01_outcome,I_normal,R0,R0_outcome,I,change_i=anvil.server.call("math",Global.initial_i,Global.suceptible,Global.contact,Global.infp,Global.contact_new)

Then, where did I go wrong? :thinking:

Did you import it in all forms where you reference it?

import Globals

The name is also CaSe SenSitive, so make sure you use the same case.

1 Like
ImportError: No module named Global

I imported but it is showing this Error

Oh Wait!. The name of the module is “Global” not “Globals”. Does that have something to with the Error?

The name of your module and the name you use in your import must match exactly. Doesn’t matter what they are called as long as they are exactly the same.

Also, make sure this is a client module, not a server module.

1 Like

I checked and it is the same…“Global”

I think I solved it but ran in to another error. I replaced

import Global 

to

from .. import Global

And then i got his error:

TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

in line 7 of the serverModule code

Hi @aadhiarun2007,
please be aware that the server does not store state. Only if you have the “keep server running”-option in your plan and activated. And only if you have declared them in a global scope and not just in a funtion. So you either need to call the server funtion with several arguments or just don’t use the server at all. The calculations of a SIR model are so simple, that you can do them in a client module. :grinning:

1 Like

Ok. But I am not using the paid version so i guess i have to do it in the client code. But I am getting the same error I got first and since i cannot use a Module, how do i fix it? Is there a way I can send Info between Forms without a module?

Hey, Guys, i fixed it by making my app print the output in the same page.

Note I did say this above :

2 Likes

Oh Sorry! i guess i skipped over that part… :sweat_smile:

1 Like