Skip to main content

Streamlit Connection for Google Sheets

Project description

Streamlit GSheetsConnection

Connect to public or private Google Sheets from your Streamlit app. Powered by st.connection() and gspread.

GSheets Connection works in two modes:

  • in Read Only mode, using publicly shared Spreadsheet URLs (Read Only mode)
  • CRUD operations support mode, with Authentication using Service Account. In order to use Service Account mode you need to enable Google Drive and Google Sheets API in Google Developers Console. Follow Initial setup for CRUD mode section in order to authenticate your Streamlit app first.

Streamlit App

Install

pip install gsheets-connector

Minimal example: publicly shared spreadsheet (read-only)

# example/st_app.py

import streamlit as st
from streamlit_gsheets import GSheetsConnection

url = "https://docs.google.com/spreadsheets/d/1JDy9md2VZPz4JbYtRPJLs81_3jUK47nx6GYQjgU8qNY/edit?usp=sharing"

conn = st.connection("gsheets", type=GSheetsConnection)

data = conn.read(spreadsheet=url, usecols=[0, 1])
st.dataframe(data)

Service account / CRUD example

Initial setup for private spreadsheet and/or CRUD mode

  1. Setup .streamlit/secrets.toml inside your Streamlit app root directory, check out Secret management documentation for references.
  2. Enable API Access for a Project
    • Head to Google Developers Console and create a new project (or select the one you already have).
    • In the box labeled “Search for APIs and Services”, search for “Google Drive API” and enable it.
    • In the box labeled “Search for APIs and Services”, search for “Google Sheets API” and enable it.
  3. Using Service Account
    • Enable API Access for a Project if you haven’t done it yet.
    • Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”.
    • Fill out the form
    • Click “Create” and “Done”.
    • Press “Manage service accounts” above Service Accounts.
    • Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.
    • Select JSON key type and press “Create”.

You will automatically download a JSON file with credentials. It may look like this:

{
    "type": "service_account",
    "project_id": "api-project-XXX",
    "private_key_id": "2cd … ba4",
    "private_key": "-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n",
    "client_email": "473000000000-yoursisdifferent@developer.gserviceaccount.com",
    "client_id": "473 … hd.apps.googleusercontent.com",
    ...
}

Remember the path to the downloaded credentials file. Also, in the next step you’ll need the value of client_email from this file.

  • :red[Very important!] Go to your spreadsheet and share it with a client_email from the step above. Just like you do with any other Google account. If you don’t do this, you’ll get a gspread.exceptions.SpreadsheetNotFound exception when trying to access this spreadsheet from your application or a script.
  1. Inside streamlit/secrets.toml place service_account configuration from downloaded JSON file, in the following format (where gsheets is your st.connection name):
# .streamlit/secrets.toml

[connections.gsheets]
spreadsheet = "<spreadsheet-name-or-url>"
worksheet = "<worksheet-gid-or-folder-id>"  # worksheet GID is used when using Public Spreadsheet URL, when usign service_account it will be picked as folder_id
type = ""  # leave empty when using Public Spreadsheet URL, when using service_account -> type = "service_account"
project_id = ""
private_key_id = ""
private_key = ""
client_email = ""
client_id = ""
auth_uri = ""
token_uri = ""
auth_provider_x509_cert_url = ""
client_x509_cert_url = ""

Code

# example/st_app_gsheets_using_service_account.py

import streamlit as st
from streamlit_gsheets import GSheetsConnection

st.title("Read Google Sheet as DataFrame")

conn = st.connection("gsheets", type=GSheetsConnection)
df = conn.read(worksheet="Example 1")

st.dataframe(df)
# .streamlit/secrets.toml

[connections.gsheets]
spreadsheet = "<spreadsheet-name-or-url>"
worksheet = "<worksheet-gid-or-folder-id>"  # worksheet GID is used when using Public Spreadsheet URL, when usign service_account it will be picked as folder_id
type = ""  # leave empty when using Public Spreadsheet URL, when using service_account -> type = "service_account"
project_id = ""
private_key_id = ""
private_key = ""
client_email = ""
client_id = ""
auth_uri = ""
token_uri = ""
auth_provider_x509_cert_url = ""
client_x509_cert_url = ""
# requirements.txt

streamlit==1.22
git+https://github.com/streamlit/gsheets-connector
pandasql  # this is for example/st_app.py only

Full example

Check gsheets_connection/example directory for full example of the usage.

Q&A

  • Does this work with a public spreadsheet without the authentication details? Or only a private spreadsheet?

    GSheets Connection works in two modes:

    • in Read Only mode, using publicly shared Spreadsheet URLs (Read Only mode)
    • CRUD operations support mode, with Authentication using Service Account. In order to use Service Account mode you need to enable Google Drive and Google Sheets API in Google Developers Console. Follow Initial setup for CRUD mode section in order to authenticate your Streamlit app first.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gsheets_connector-1.4.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gsheets_connector-1.4.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file gsheets_connector-1.4.0.tar.gz.

File metadata

  • Download URL: gsheets_connector-1.4.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for gsheets_connector-1.4.0.tar.gz
Algorithm Hash digest
SHA256 e970570ea4c42e28b390c94d8dfb9b17a05ff01c1ddd9acdb664c35fe03b7581
MD5 3e4db41239efe477b81398615060d5c4
BLAKE2b-256 0f76c6e62f3b157fcfa7f361e5358bed0c36e659db946f0dad80d860c2f0dc8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for gsheets_connector-1.4.0.tar.gz:

Publisher: publish.yml on xiaolou86/gsheets-connector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gsheets_connector-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gsheets_connector-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a204f5f42a166997fbf329ec6e7b563a410aa3cc4568a4157481d0b793728d8e
MD5 064b49359eced1f6d0f1681d98f2dc11
BLAKE2b-256 f3582020a83499bcec8961089db4c2bcf2d83b09eaef1d6e93acf8a78c28be07

See more details on using hashes here.

Provenance

The following attestation bundles were made for gsheets_connector-1.4.0-py3-none-any.whl:

Publisher: publish.yml on xiaolou86/gsheets-connector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page