@st.cache_data VS @st.cache_resource - small issues

@TomJohn for the first issue with the Google Sheet example - I found that this was due to gsheetsdb Rows object which is returned being not serializable. I tested a few other DB API implementations (Postgresql, SQLite) and they did not have this issue. It also seems like gsheetsdb is a bit stale so maybe not the best to use in our example.

I pulled down your flashcards app and was able to get it working with a much simpler pandas pd.read_csv() approach

import pandas as pd

@st.cache_data(ttl=600)
def load_data(sheets_url):
    csv_url = sheets_url.replace('/edit#gid=', '/export?format=csv&gid=')
    return pd.read_csv(csv_url)


# ok let's load the data
questions_df = load_data(st.secrets["public_gsheets_url"])

With this approach you retrieve the values a little differently but itโ€™s pretty close. Use questions_df.iloc[st.session_state.q_no].Question instead of rows[st.session_state.q_no].Question, for example.

I filed a bug to fix the tutorial: Example code in public google sheets tutorial is broken ยท Issue #589 ยท streamlit/docs ยท GitHub

2 Likes