I have 5 functions setup, and the first 3 establish the connection to databases, and collect data, and do some manipulations. The next two manipulate the dataset based on variables passed into them. Whenever any variable changes via a widget, I’m returned WARNING: Cached Object Mutated, and the 3rd function (that should create a static dataset) gets re-run. Here’s some dummy code.
@st.cache(allow_output_mutation=True)
def connect_engine(url, conn_args=None):
if conn_args != None:
engine = sqlalchemy.create_engine(url, connect_args=conn_args)
else:
engine = sqlalchemy.create_engine(url)
return engine
@st.cache(hash_funcs={sqlalchemy.engine.base.Engine: id}, suppress_st_warning=True)
def get_data_1(engine):
sql = "SELECT * FROM table"
df = pd.read_sql(sql, engine)
return df
@st.cache(hash_funcs={sqlalchemy.engine.base.Engine: id}, suppress_st_warning=True)
def get_data_2(dataframe, engine):
sql = "SELECT * FROM table"
df = pd.read_sql(sql, engine)
dataframe['col_2'] = df['col_1'].add(3)
return df
@st.cache(allow_output_mutation=True)
def add_more(dataframe, var=True):
if var:
dataframe['col_2'] = dataframe['col_2'].add(3)
else:
pass
return df
db1 = connect_engine(connection_url1)
db2 = connect_engine(connection_url2)
df = get_data_1(db1)
df = get_data_2(df, db2)
if st.checkbox('Use variable'):
variable=True
else:
variable=False
df = add_more(df, variable)
st.write(df.head()