How to rerun only a part of my program

Hi i have a program that is slow to execute (i think) and i would like to run it faster.

I already did:
@st.cache_data to load data just once but i would like also to do that for graph etc. I mean i have some st.selectbox that change the output.
So my question is how to not run the code that follow if i do not change this st.selectbox option ? That could update my program and make it run faster
Or maybe somenone has better solution for me than the idea i have in my mind?

I think you are looking for fragments

For example, i am displaying a dataframe based on selected period. It seems that the code is completely run whereas i did it already and it is just a display instruction.

I would like not to rerun the code

    cols[0].write("**Classement des fonds selon Ranking :**")
    choix_periode = cols[0].selectbox("Choix de la période pour affichage des résultats",
                                      ["1 an", "3 ans", "5 ans"],
                                      index=0)
    colonne_afficher = [col for col in classt.columns if choix_periode in col]
    colonne_afficher = ["fund_name"] + colonne_afficher + ['Rank perf', 'Rank risque', 'Rank régularité', 'Rank']
    
    cols[0].dataframe(classt
                      .loc[:, colonne_afficher]                      
                      .sort_values("Rank", ascending=False), 
                      column_config={"fund_name": "Nom du fonds",
                                     },
                      use_container_width=True, 
                      hide_index=True
                      )

First, it is not just a display instruction, it contains data selection and sorting too.

Second, that code would rerun only when a period is selected, which looks just right to me.

You mean that st.experimental_fragment is the right solution to me?
I need to do a function with code like that. That’s correct ???

@st.experimental_fragment
def data():
    cols[0].write("**Classement des fonds selon Ranking :**")
    choix_periode = cols[0].selectbox("Choix de la période pour affichage des résultats",
                                      ["1 an", "3 ans", "5 ans"],
                                      index=0)
    colonne_afficher = [col for col in classt.columns if choix_periode in col]
    colonne_afficher = ["fund_name"] + colonne_afficher + ['Rank perf', 'Rank risque', 'Rank régularité', 'Rank']
    
    cols[0].dataframe(classt
                      .loc[:, colonne_afficher]                      
                      .sort_values("Rank", ascending=False), # I can delete this line
                      column_config={"fund_name": "Nom du fonds",
                                     },
                      use_container_width=True, 
                      hide_index=True
                      )

I couldn’t tell for sure. It seems to be the answer to the question in the title: “How to rerun only a part of my program”.

What happened when you tried?

I think the effect of that would be that, when the user interacts with the selectbox, only the function data reruns, but not the rest of the code. But I have never used fragments.

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