Relationship between widgets

Hi! I’m new to Streamlit

So, in my project I have two tabs. In “tab1”, I have a “button” that, if clicked, calculates stuff and outputs graphics. Alright, that is already done.

In “tab2” I have a “selectbox” that also will output some stuff based on the selection of the user but, also, based on some variables that are calculated after the button is clicked on “tab1”

The problem is, as far as I know, everytime an user interacts with the “selectbox” it runs the whole code and all things that were calculated “tab1” disappear, I guess because they are all inside an “if button”.

My goal is to avoid it, the logic is:

1- If button on “tab1” => apply some lines of code that calculates things and show graphics on “tab1”
2- If something is selected on the “tab2”, after the button is clicked at least once => apply some other specific lines of code that outputs some information on “tab2” without clearing “tab1”

I don’t know If I made myself clear, I’m sorry if it’s confusing. I believe that the solution is something with session states or cache_data, but I’ve been trying with it and no success.

Thanks for your attention!

Sounds like you want the button to recompute something when clicked, but keep displaying the results as the user navigates elsewhere.

Instead of directly displaying something within “if button”, use the button to run your computation and save the results to a key in session state. Then, independent of the button, display the results stored in session state.

import streamlit as st

if 'result' not in st.session_state:
    st.session_state.result = None

something = st.text_input('Something')

if st.button('Compute Something'):
    # Do a computation or refresh the result you want to display
    st.session_state.result = something

if st.session_state.result is not None:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.