Skip to content

Example code in public google sheets tutorial is broken #589

@sfc-gh-jcarroll

Description

@sfc-gh-jcarroll

Summary

After the update to cache_data() this code is no longer working. As reported here

Steps to reproduce

  1. Set up and run the code from the example

Expected behavior:

It works!

Actual behavior:

You get an UnserializedReturnValueError

Is this a regression?

Yes

Debug info

Additional information

In investigating the issue I also noticed that the recommended library, gsheetsdb is more or less marked as deprecated and hasn't been updated in a couple years. Also I had issues using the suggested replacement library.

Instead, I think we should just use the simpler pd.read_csv method which requires no additional new installs. Here's an updated example that works:

# streamlit_app.py

import streamlit as st
import pandas as pd

# Read in data from the Google Sheet.
# Uses st.cache_data to only rerun when the query changes or after 10 min.
@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)


df = load_data(st.secrets["public_gsheets_url"])

# Print results.
for _, row in df.iterrows():
    st.write(f"{row.name} has a :{row.pet}:")

I'd suggest to re-write the page with this example and remove the mention of the 3rd party install. You can verify the above code with this secrets.toml value:

public_gsheets_url = "https://docs.google.com/spreadsheets/d/1NQlj3nZQF_78S-LjfATGACQRtlzIRCuaBb7Y8tN1hO4/edit#gid=0"

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions