I have a question about streamlit best practices. I am making a rather large multipage streamlit app, containing a lot of widgets and a lot of state variables.
At the moment, the state is updated directly, where needed. For instance if a button is pressed, the on_click calls a method where I might do something like
def authenticate_user(): #some logic st.session_state.show_successful_authentication_message = True
I kind of don’t love how the state is updated so explicitly (and with such cumbersome text) everywhere in the codebase, like in the example above. Even when I render pages, I do a lot of conditional rendering and do things like:
if st.session_state.show_successful_authentication_message: st.success("You have authenticated successfully!")
I am thinking of creating a separate file (and call it, say, “state_management.py”) where I define methods to interact with the session state, and then I would call those methods from the relevant files. The file would look something like this
show_successful_authentication_message(): st.session_state.show_successful_authentication_message = True authentication_message_is_visible(): return st.session_state.show_successful_authentication_message
and then I could change the above code to
from state_management.py import show_successful_authentication_message def authenticate_user(): #some logic show_successful_authentication_message()
from state_management.py import authentication_message_is_visible if authentication_message_is_visible(): st.success("You have authenticated successfully!")
Do you think this would be a good approach? Basically, the idea is to put all the interactions with the session state in custom methods in a separate file, and import those methods where needed instead of having explicit and direct interactions with the session state happening all over the place in the codebase, which can get quite bug-prone and messy (and looks very messy too?)
What do you think? And is there some other established best practice / way of solving this problem I am not aware of? I’d love to hear your thoughts!