Gspread and st.cache_data

I am using the following code to connect to google sheets:

# Set up credentials to access Google Sheets API
scope = ['', '']
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(creds)

sheet_name = 'x'
sheet ='y').worksheet(sheet_name)
df = pd.DataFrame(sheet.get_all_records())

But I didn’t understand how to use st.cache_data to update data frequently. The spreadsheet data is updated every other day, I wanted the charts to keep up with those updates. Could anybody help me, please?


hey @wagnerbrito! So st.cache_data has a ttl parameter that stands for time to live, which means you can tell st.cache_data to only allow data to live in there for any amount of time that you like!

The way you’d implement this would be something like

@st.cache_data(ttl=3600) #this caches the data for one hour max
def get_all_records(sheet):
df = pd.DataFrame(sheet.get_all_records)

1 Like

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