How to print data without to relaunch the st.button

Summary

How to

Steps to reproduce

Code snippet:

def analyse_style_histo(numero, classe):
    # Some stuff from internet
    return df

@st.cache_data # I tried this but without success !
def analyse_style(numero, combo):
    # Some stuff from internet
    return df, r2

def graph_style(df):
    return fig

if st.button('Analyse de style', type="primary"):
    st.subheader(f"Analyse de style pour le fonds {fonds}")
    if classe=='Actions':
        style = analyse_style_histo(numero_fonds, 
                                    classe)
        analyse_style, r2 = analyse_style(numero_fonds, 1)

        col = st.columns(2)
        col[0].caption('Analyse style par pays')
        if len(style[1])!=0:  #Test if style is empty data
            col[0].plotly_chart(graph_style(style[1]), use_container_width=True)
            periode = col[1].radio("PΓ©riode",
                                   analyse_style.columns, 
                                   horizontal=True) # buttons to choose which column to print 
            col[1].dataframe(analyse_style[periode]) # How to print this data without launching again the st.button()
        else: 
            col[0].write('Analyse non pertinente')        

My question is simple: how can I do the analysis without to have to relaunch the st.button to print analyse_style[periode] ?

Thanks.

1 Like

Hi @Jacques2101! :wave:

To prevent the whole script from rerunning each time a button is pressed in Streamlit, you can use st.session_state to store and access data across reruns. This way, after initially fetching the data, you can just retrieve it from st.session_state in future runs, avoiding unnecessary recalculations and fetching.

You will need to adjust your script to incorporate st.session_state. Feel free to reach out if you need any assistance with this! :slightly_smiling_face:

Good luck! :raised_hands:

Charly

Thx !
Do you have examples how to use it especially in my case ?

Cordialement

I can try! :smiley:

Could you give me the full code as well as dummy datasets that I could experiment with?

Charly