Using user input as global variable in One server

Hi! I’m trying to use user input as a global variable in my app server for those variables to be used by different functions. The user inputs some parameters in different textboxes and when he clicks on button_1, a graph appears with the different parameters as input. As of now, If I define the parameters in the server as global variables, the graph does appear when the button is pushed. The goal is to make it interactive

I tried defining the variables in the button_1_click() and calling the make_plot (Client side), but only the xy plane is displayed, without the graphical function:
def button_1_click(self, **event_args):
“”“This method is called when the button is clicked”""
t = self.text_box_1.text
a = self.text_box_2.text
s = self.text_box_3.text
d = self.text_box_4.text
n = self.text_box_5.text
anvil.server(s, t, a, d, n)
# Any code you write here will run when the form opens.
media_obj = anvil.server.call(‘make_plot’,s , t, n, d, a)
self.image_1.source = media_obj
pass

Here is the server code:

import anvil.server
import anvil.mpl_util
import numpy as np
import matplotlib.pyplot as plt
from anvil import *

#s = 0.3
#d = 0.03
#n = 0.02
#a = 0.3
#t = 50

KAPITAL_0 = 1
POP_0 = 1

@anvil.server.callable
def make_plot(s):

Make a nice wiggle

x = capital_capita(t ,s ,d ,n ,a)[3]
y = capital_capita(t ,s ,d ,n ,a)[1]

Plot it in the normal Matplotlib way

#plt.figure(1, figsize=(10,5))
plt.plot(x, y)

Return this plot as a PNG image in a Media object

return anvil.mpl_util.plot_image()

def get_kapital_dot(k, s, d, n, a):
return s*(k**a)-(n+d)*k

Solution to Diff equation

def capital_capita(t, s, d, n, a):
year = []
capital_list = []
capital_g_list = []
# Initialize changing values
k = KAPITAL_0
delta_t = 1
current_year = 0
for time in np.arange(current_year, t, delta_t):
capital_g_list.append(get_kapital_dot(k, s, d, n, a)/k)
k += get_kapital_dot(k, s, d, n, a) #* delta_t

    current_year += delta_t
    year.append(current_year)
    capital_list.append(k)

return k, capital_list, capital_g_list, year

I thing the problem might be that when I use anvil.server.call(‘make_plot’,s , t, n, d, a), the other functions that are in the server and that are used inside the make_plot() function might not receive the s, t, n, d, a variables since they are used locally? Thus my question: Is there a way to define global variables in one server using user input?
Thanks @jshaffstall for user etiquette

https://anvil.works/build#clone:PY63SPEA4LRWAWC2=GMG7QXTOTS4USB52BLAXGUVN

A couple of things here.

When you call make_plot on the client side, you pass it the following arguments: s, t, n, d, & a , but you have written the server function to only accept s , so it will do nothing with t, n, d, & a.

One possible solution would be two rewrite the function to accept all of those variables.

so this:

def make_plot(s):
  # Make a nice wiggle
  
  x = capital_capita(t ,s ,d ,n ,a)[3]
  y = capital_capita(t ,s ,d ,n ,a)[1]
  
  # Plot it in the normal Matplotlib way
  #plt.figure(1, figsize=(10,5))
  plt.plot(x, y)  
  
  # Return this plot as a PNG image in a Media object
  return anvil.mpl_util.plot_image()

will become this:

def make_plot(t ,s ,d ,n ,a):
  # Make a nice wiggle
  
  x = capital_capita(t ,s ,d ,n ,a)[3]
  y = capital_capita(t ,s ,d ,n ,a)[1]
  
  # Plot it in the normal Matplotlib way
  #plt.figure(1, figsize=(10,5))
  plt.plot(x, y)  
  
  # Return this plot as a PNG image in a Media object
  return anvil.mpl_util.plot_image()

Then you will always be using the user input as your variables instead of the global constants defined above.

About Global Variables

You could define global variables and have some set_global_variables method to define the variables. The issue with this is you would need a persistent server to hold those values. Otherwise, every server call would have those variables reset.

The persistent server is only in the business plan and above.

You can always store the data in server sessions or cookies if that was what you were looking for.

Also, your code will look much more neat if you properly format it by adding ``` before it

2 Likes

Corrected it, thanks for the info about global variables, i’ll try another method then!

1 Like

I don’t think you really need global variables here. As @lsaenz9104 suggests, just pass your user input into the server functions that need them.

The only real reason to want to use global variables is to cache data that’s large enough that it would slow things down to pass it every time you call a server function. If you have data that large, use sessions instead as @divyeshlakhotia suggests. Store it on the first call and access the session data on subsequent calls.

Anvil server calls are really a different model than most Python programs. The entire Python interpreter exists only for the duration of a single server call, and is recreated from scratch for the next server call. Session variables are persisted between those calls.

1 Like