Help: programatically turn off st.fragment based on a condition

I have a function:

@st.experimental_fragment(run_every=“30s”)
def plot_line_chart_v2

Multiple charts are being drawn using the above function is it poassible to set st.fragment programatically to true based on a condition.
Please suggest.
Thanks

Hi @Sai_SaiGraph,

Thanks for sharing this question!

There’s no direct way to set this dynamically because @st.experimental_fragment decorator is applied at function definition time and not during runtime.

You can work around this by defining 2 separate functions and using a condition to call the appropriate one. The functions could look like this:

@st.experimental_fragment(run_every="30s")
def plot_line_chart_v2_fragment():
    df = get_data()
    st.line_chart(df)

# Regular function
def plot_line_chart_v2():
    df = get_data()
    st.line_chart(df)

Hi @Sai_SaiGraph,

I don’t know if this helps, but I have something similar. For my case, I wanted a new container with some specific analysis to show if the user selects an item in one of my selectboxes.

@st.experimental_fragment
def user_activity_graph(dataframe):
    "analysis to be performed"
    return analysed_dataframe

Use in streamlit

product_list = st.multiselect(options=unique_product_list, label="Products", placeholder="Select product")

if product_list:
    with st.container(border=True):
        st.subheader('XXXXs')
        user_activity_graph(users_activities_df)

So basically, this container will be shown only if an item is selected from the product_list.

Hope this helps.