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