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.
import streamlit as st
import pandas as pd
from google.oauth2.service_account import Credentials
scopes = [
skey = st.secrets["gcp_service_account"]
credentials = Credentials.from_service_account_info(
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.
def load_data(url, sheet_name="Transactions"):
sh = client.open_by_url(url)
df = pd.DataFrame(sh.worksheet(sheet_name).get_all_records())
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.