How do I instantiate a class object without it being a global variable?

Hi there,

I am building an input form that takes in the user input and store the inputs within the class. And upon user clicking submit, I then turn the input class into a json object. This worked perfectly in local but when I deployed my app on Azure, it seems to have issues, not sure why but I think it could be due to the Input class being a global variable, and hence when user is changing the input form and clicking save, it is instead saving different values and not the intended ones.

My inputclass.py:

class InputClass:
def init(self, a: Optional[str], b: Optional[str]):
self.a = a
self.b = b
def _set_a(self, a: Optional[str]):
self.a = a
def _set_b(self, b: Optional[str]):
self.b = b
… other methods

And my page.py:

from inputclass import InputClass

input_store = InputClass(
a=“something_a”,
b=“something_b”,
)

layout = dmc.Grid(
children=[
dmc.GridCol(
children=input_form(input_object),

),…
]
)

@callback(
Input(“input_a”, “value”)
)
def change_a(value):
input_store._set_a(value)

and other callbacks here…

But this code seems to have issues when deployed or hosted on Azure but locally it runs perfectly fine. Is there a way to avoid this sharing of global variable? I would like such that each user has their own session and they could fill their inputs independantly. Such that each web app instance opened on browser is its separate web app independant state/session.

Any tips or suggestions would be really helpful. Thanks a lot!

Hi @jasonsusanto

Have you tried an All-In-One component?

Hi AnnMarieW,

Thanks for the reply. I have not tried an All-in-One component yet. I am still new to coding in python as well (usually coding in React TS, hence implementing similar concepts in Dash python, thought it would work :sweat_smile:). Can I know why would the all-in-one component be useful in this scenario? And would it be possible otherwise to have the instantiation of the input_store = InputClass(…) under a function and save it in a dcc.Store()? Would this be possible or would it be better if I use the All-in-One component for input form handling? Lastly, the input form that I will be using would just be for a single page in the app.

Cheers!

The AIO component is a way to bundle s components and callbacks in way that can be easily reused. To see an example see this post - it look similar to what you are trying to do:

1 Like