Ag-Grid checkbox selection reruns entire script

Hi all. I’m trying to implement an app with 3 steps like this

Step 1: Load the dataset

Step 2: Filter some rows

Step 3: Print the filtered rows

For that, this is my current Python script (this is just an example):

import streamlit as st
from st_aggrid import AgGrid,GridOptionsBuilder
import pandas as pd
from time import sleep
# Step 1
def load_data():
    df = pd.read_csv('')
    return df
df = load_data()
gd = GridOptionsBuilder.from_dataframe(df)
gd.configure_default_column(editable=True, filter=True, resizable=True, sortable=True, value=True, enableRowGroup=True,
enablePivot=True, enableValue=True, floatingFilter=True, aggFunc='sum', flex=1, minWidth=150, width=150, maxWidth=200)
gd.configure_selection(selection_mode='multiple', use_checkbox=True)
gd.configure_column('Passengerid', headerCheckboxSelection = True)
gridoptions =
tabla = AgGrid(df, editable=True,height=300,width='100%',

# Step2: Select some rows
b2 = st.checkbox("Filter")
if b2:
    sel_row = tabla["selected_rows"]
    if sel_row:
        df_filter = pd.DataFrame(sel_row)
# Step 3: Print rows selected

The problem is that every time I select some rows with the checkbox, the entire script re runs. I put the sleep(10) just to verify that the function is actually re-running.

Is there any way, like a Python trick, to “freeze” some part of the code and avoid this? I know that streamlit works like that; you touch something and the script re-runs. But maybe there’s a way to just run the Step 1 part only the first time the app runs and then when you touch something and the script re-runs it just skips the entire Step 1 block.

Thank you in advance

You can’t do that. But usually you can arrange your code so that re-running doesn’t become “the problem”. In your case, if re-running is too slow, the bottleneck is probable the data loading. Then you can cache the data so that reruns are faster.