Connect Streamlit to a public Google Sheet

This guide explains how to securely access a public Google Sheet from Streamlit. It uses st.connection, Streamlit GSheetsConnection, and Streamlit's Secrets management.

This method requires you to enable link sharing for your Google Sheet. While the sharing link will not appear in your code (and actually acts as sort of a password!), someone with the link can get all the data in the Sheet. If you don't want this, follow the (more complicated) guide to Connect Streamlit to a private Google Sheet.

This tutorial requires streamlit>=1.28 and st-gsheets-connection in your Python environment.

If you already have a Sheet that you want to access, you can skip to the next step. See Google's documentation on how to share spreadsheets for more information.

Create a spreadsheet with this example data and create a share link. The link should have "Anyone with the link" set as a "Viewer."

namepet
Marydog
Johncat
Robertbird
screenshot 1
screenshot 1

Your local Streamlit app will read secrets from a file .streamlit/secrets.toml in your app's root directory. Create this file if it doesn't exist yet and add the share link of your Google Sheet to it as shown below:

# .streamlit/secrets.toml [connections.gsheets] spreadsheet = "https://docs.google.com/spreadsheets/d/xxxxxxx/edit#gid=0"
priority_high

Important

Add this file to .gitignore and don't commit it to your GitHub repo!

Copy the code below to your Streamlit app and run it.

# streamlit_app.py import streamlit as st from streamlit_gsheets import GSheetsConnection # Create a connection object. conn = st.connection("gsheets", type=GSheetsConnection) df = conn.read() # Print results. for row in df.itertuples(): st.write(f"{row.name} has a :{row.pet}:")

See st.connection above? This handles secrets retrieval, setup, query caching and retries. By default, .read() results are cached without expiring. You can pass optional parameters to .read() to customize your connection. For example, you can specify the name of a worksheet, cache expiration time, or pass-through parameters for pandas.read_csv like this:

df = conn.read( worksheet="Sheet1", ttl="10m", usecols=[0, 1], nrows=3, )

In this case, we set ttl="10m" to ensure the query result is cached for no longer than 10 minutes. You can also set ttl=0 to disable caching. Learn more in Caching. We've declared optional parameters usecols=[0,1] and nrows=3 for pandas to use under the hood.

If everything worked out (and you used the example table we created above), your app should look like this:

Finished app screenshot

This tutorial assumes a local Streamlit app, however you can also connect to Google Sheets from apps hosted in Community Cloud. The main additional steps are:

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.