How to refresh my connection with sqlserver

Hi everyone~
For start great thanks for everyone how are working with Streamlit, It is a great way to make Data app.
I encountered with a problem when I try to make a KPI Dashboard app:

@st.cache()
def load_sql():
    conn = pymssql.connect(**,**,**,**)
    sql =''' select * from test_table '''
    retrun pd.read_sql(sql, conn)

data = load_sql(conn)

But my ‘test_table’ update daily, so is there a way that it can automatically refresh every night?

PS: I tried to add a ’ st.button ’ on the page to rerun this load_sql() function, but it doesn’t seem to work.

Can anyone please HELP? :joy:

Hi @oliverjia, welcome to the Streamlit community!

st.cache has an argument ttl that specifies how long the result should be cached for. So while it will not refresh automatically, if the result has been in cache longer than the ttl argument worth of seconds, then it will re-pull the data from the database. You can see an overall example of this pattern in our documentation:

https://docs.streamlit.io/en/stable/tutorial/postgresql.html?highlight=ttl#write-your-streamlit-app

Best,
Randy

1 Like

Much appreciated!! Let me try it now :stuck_out_tongue_closed_eyes: