So I tried using this documentation (Connect Streamlit to a private Google Sheet - Streamlit Docs) to pull data from a private google worksheet of financial data, but gsheetsdb was causing issues (and I think it’s deprecated). Also, I wanted to pull the data as a pandas DataFrame instead of using sql commands. Here is the solution I found, while still using the secrets.toml approach in the main documentation.
First step is to follow about 90% of the guide Connect Streamlit to a private Google Sheet - Streamlit Docs except, in particular, about setting up the google cloud env and adding the shared email to your private sheet. Just ignore the last bit of code at the end. Instead do this:
import streamlit as st
import pandas as pd
import gspread
from google.oauth2.service_account import Credentials
scopes = [
"https://www.googleapis.com/auth/spreadsheets",
]
skey = st.secrets["gcp_service_account"]
credentials = Credentials.from_service_account_info(
skey,
scopes=scopes,
)
client = gspread.authorize(credentials)
# Perform SQL query on 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(url, sheet_name="Transactions"):
sh = client.open_by_url(url)
df = pd.DataFrame(sh.worksheet(sheet_name).get_all_records())
return df
Now just call load_data(…) and it will return a pandas dataframe. As usual, make sure to update your requirements.txt with gspread. One caveat is that the .get_all_records function didn’t like when the sheet had empty column in between non empty ones. So I deleted those and everything seemed to work. Hope this helps someone.
Hi! After that, how can I check if the code is reading my google spreadsheet correctly? I’m a beginner in streamlit and your method seemed easier to me. I haven’t deployed yet, I’m testing locally before publishing.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.