Thanks Thiago, I now understand when a function will be re-run; it makes sense.
You are correct, the two cached functions were being cached properly and not rerunning. My mistake.
The connection establishment is being re-run each time, which was causing the responsiveness to be slow (about one second lag per action). Originally, I tried wrapping the connection establishment in a function and caching it, but I ran in to errors because the connection and cursor objects cannot be hashed. I tried adding “ignore_hash=True” argument, but still got an error.
import streamlit as st
import pandas as pd
import psycopg2
@st.cache()
def get_cursor():
con = psycopg2.connect(dbname='dbname',
host='host',
port='0000', user='user', password='pass')
return con.cursor()
@st.cache()
def load_names(cur):
cur.execute("SELECT DISTINCT name FROM dev_all.ds_logs")
return cur.fetchall()
@st.cache()
def load_data(cur, app):
cur.execute("SELECT * FROM dev_all.ds_logs WHERE name = '{}'".format(app))
return cur.fetchall()
cur = get_cursor()
st.title('Logs Explorer')
apps = load_names(cur)
app = st.selectbox("Select App", [str(app[0]) for app in apps])
data = load_data(cur, app)
logs = pd.DataFrame.from_records(data)
st.write(logs)
Error:
Streamlit cannot hash an object of type <class 'psycopg2.extensions.connection'>.,
**More information:** to prevent unexpected behavior, Streamlit tries to detect mutations in cached objects so it can alert the user if needed. However, something went wrong while performing this check.
Please [file a bug](https://github.com/streamlit/streamlit/issues/new/choose).
To stop this warning from showing in the meantime, try one of the following:
* **Preferred:** modify your code to avoid using this type of object.
* Or add the argument `ignore_cache=True` to the `st.cache` decorator.
I did try ignore_cache=True as well, but it didn’t even recognize the argument (pretty sure that wouldn’t fix the issue anyway) (perhaps the error message intended to suggest using ignore_hash, not ignore_cache?).
I don’t see a way to avoid using a connection object, and if I leave it as global it makes every action lag for 1 second as it reconnects.
Thanks for your help. I hope that resolving this issue can help others, because Streamlit is an awesome concept and I’m excited to use it on a bunch of projects!
edit: I wonder if it would be useful to have a way to explicitly tell streamlit not to re-run certain variables or lines of code.