The plot reloads every time I move across st.radio buttons

I am building a streamlit app which has two radio buttons, and there is a plot in each one of them. I would like to understand if it is normal behavior that the plots get rebuilt every time I move from one radio button to another, although I cache the plots. My expectation is that once the plot is rendered once, if I move to another radio button and return, it should remain displayed as I already have rendered it. Since, the dataset is quite big (400MB), it takes a while to re-render the plot each time.

Please see the code for more info:


combined_file = st.sidebar.file_uploader('Upload combined file here!') 
col1, col2, col3 = st.columns([0.3, 0.3, 1.4])
    with col1:
        # User input of the threshold parameters
        threshold = st.number_input('X threshold')
        
    if combined_file is not None:
        @st.experimental_memo
        def upload_file(uploaded_file):
            try:
                df = pd.read_csv(uploaded_file)
            except Exception:
                pass
                print('The file type is not recognised. Please upload CSV file!')
            return df 

        df = upload_file(combined_file)          

        # radio buttons  
        with col2:  
            plot_1 = 'Histograms'
            plot_2 = 'Plots'
            sub_options = st.radio('Options', options = [plot_1, plot_2])  

            if sub_options == plot_1:                
                with col3:          

                    @st.cache(hash_funcs={dict: lambda _: None})
                    def histogram_plot():
                        fig_histogram = make_subplots(rows=2, cols=2)

                        block_position = go.Histogram(x = Pos,
                                    name = 'Position',
                                    xbins=dict(start=0))

                        fig_histogram.update_layout(height=600,
                                            width=600,
                                            #title_text = "Histograms"
                                            )
                        cached_dict = {'fig_histogram': fig_histogram}
                        return cached_dict

                    def display_histogram():
                        charts = histogram_plot()
                        st.plotly_chart(charts['fig_histogram'],
                                        use_container_width=True,
                                        theme="streamlit")
                    display_histogram()

            if sub_options == plot_2:
                with col3:     
                    col_x = 'Elapsedtime'
                    col_list_top_plot = ['WOB', 'RotRPM']                
                    @st.cache(hash_funcs={dict: lambda _: None})
                    def wob_plot():       
                        fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.03)

                        for i in range(len(col_list_top_plot)):
                            fig.add_trace(go.Line(x = concat_df[col_x],
                                                y = concat_df[col_list_top_plot[i]],
                                                name = col_list_top_plot[i]),
                                                row = 1,
                                                col = 1)

                        fig.update_layout(height=600,
                                        width=600,
                                        #title_text = "WOB Correction Subplots"
                                        )

                        cached_dict = {'fig1': fig}
                        return cached_dict
                    
                    def display_wob_plot():
                        charts = wob_plot()
                        st.plotly_chart(charts['fig1'], use_container_width=True, theme="streamlit")

                    display_wob_plot()

@st.cache is being deprecated, and tends to have more issues than the newer cache functions, so can you try st.cache_data - Streamlit Docs instead?

If it is a very large plot, you may not see a significant improvement in plotting, but it’s worth a shot.

You could also switch from radio buttons to tabs, which are visual selectors, and don’t trigger app reruns.

Thanks @blackary - I will definitely try it.

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