Skip to content
Advertisement

Streamlit python variable declaration

I wrote some dummy code to replicate my problem. I need to do some simple computations in Streamlit, but I can’t seem to find out how they handle variables and how to store info Here is my example:

JavaScript

I get the error:

JavaScript

What am I doing wrong?

Advertisement

Answer

The question has nothing to do with Streamlit, the variables are just not defined. Let me rewrite your code without the Streamlit part and with added comments to see if that clarifies things:

JavaScript

Does the error make more sense? If you want to add foo and bar, they must be defined first, which is not the case here.

If you are sure that the code will be called with options A and B before option C, then it should work. But then you’d be better off setting some default values beforehand, like foo = None and bar = None. You’d still get an error if they are still None at the C step, but that would be clearer.

Or maybe you what you are looking for are session states?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement