Skip to content

Commit

Permalink
Merge pull request #27 from masani1989/gsheet-conn-fix
Browse files Browse the repository at this point in the history
Fix for Gsheets connection based on streamlit changes
  • Loading branch information
sfc-gh-zblackwood authored Aug 15, 2024
2 parents 6d50cc1 + 1e09c42 commit cd9a6ba
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,16 @@ jobs:
python-version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r test-requirements.txt
- name: Lint with ruff
run: |
# stop the build if there are Python syntax errors or undefined names
ruff --select=E9,F63,F7,F82 --target-version=py39 .
# default set of ruff rules with GitHub Annotations
ruff --target-version=py39 .
- name: Check types with mypy
run: |
mypy --ignore-missing-imports .
- name: Test with pytest
run: |
pytest
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.5.6"
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
args: [--config=pyproject.toml]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.1
hooks:
- id: mypy
language_version: python3.8

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: end-of-file-fixer
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Streamlit GSheetsConnection

Connect to public or private Google Sheets from your Streamlit app. Powered by `st.experimental_connection()` and [gspread](https://github.com/burnash/gspread).
Connect to public or private Google Sheets from your Streamlit app. Powered by `st.connection()` and [gspread](https://github.com/burnash/gspread).

GSheets Connection works in two modes:

Expand All @@ -26,7 +26,7 @@ from streamlit_gsheets import GSheetsConnection

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

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

data = conn.read(spreadsheet=url, usecols=[0, 1])
st.dataframe(data)
Expand Down Expand Up @@ -99,7 +99,7 @@ from streamlit_gsheets import GSheetsConnection

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

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

st.dataframe(df)
Expand Down
6 changes: 3 additions & 3 deletions examples/Public_Sheet_Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

from streamlit_gsheets import GSheetsConnection

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

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

st.write("#### 2. Query public Google Worksheet using SQL")
st.info(
"Mutation SQL queries are in-memory only and do not results in the Worksheet update.",
icon="ℹ️",
icon="ℹ️", # noqa: RUF001
)
st.warning(
"""You can query only one Worksheet in provided public Spreadsheet,
Expand All @@ -34,7 +34,7 @@

from streamlit_gsheets import GSheetsConnection

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

df = conn.query('select births from "Example 2" limit 10', spreadsheet=url)
st.dataframe(df)
30 changes: 15 additions & 15 deletions examples/pages/Service_Account_Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from streamlit_gsheets import GSheetsConnection

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

Expand All @@ -36,7 +36,7 @@
and enable it.
3. [Using Service
Account](https://docs.gspread.org/en/v5.7.1/oauth2.html#for-bots-using-service-account)
* Enable API Access for a Project if you havent done it yet.
* 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
Expand All @@ -58,12 +58,12 @@
...
}}
```
Remember the path to the downloaded credentials file. Also, in the next step youll need
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 dont do this, youll get a
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.
Expand Down Expand Up @@ -105,7 +105,7 @@
from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
conn = st.experimental_connection("gsheets", type=GSheetsConnection)
conn = st.connection("gsheets", type=GSheetsConnection)

# Demo Births DataFrame
df = psql.load_births()
Expand All @@ -118,7 +118,7 @@
data=df,
)
st.cache_data.clear()
st.experimental_rerun()
st.rerun()

# Display our Spreadsheet as st.dataframe
st.dataframe(df.head(10))
Expand All @@ -127,7 +127,7 @@
st.write("#### 4. Read Google WorkSheet as DataFrame")
st.info(
"If the sheet has been deleted, press 'Create new worksheet' button above.",
icon="ℹ️",
icon="ℹ️", # noqa: RUF001
)

with st.echo():
Expand All @@ -136,7 +136,7 @@
from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
conn = st.experimental_connection("gsheets", type=GSheetsConnection)
conn = st.connection("gsheets", type=GSheetsConnection)

# Read Google WorkSheet as DataFrame
df = conn.read(
Expand All @@ -157,7 +157,7 @@
from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
conn = st.experimental_connection("gsheets", type=GSheetsConnection)
conn = st.connection("gsheets", type=GSheetsConnection)

# Demo Meat DataFrame
df = psql.load_meat()
Expand All @@ -170,15 +170,15 @@
data=df,
)
st.cache_data.clear()
st.experimental_rerun()
st.rerun()

# Display our Spreadsheet as st.dataframe
st.dataframe(df.head(10))

st.write("#### 6. Query Google WorkSheet with SQL and get results as DataFrame")
st.info(
"Mutation SQL queries are in-memory only and do not results in the Worksheet update.",
icon="ℹ️",
icon="ℹ️", # noqa: RUF001
)


Expand All @@ -188,7 +188,7 @@
from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
conn = st.experimental_connection("gsheets", type=GSheetsConnection)
conn = st.connection("gsheets", type=GSheetsConnection)

# make sure worksheet name is in double quota "", in our case it's "Example 1"
# DuckDB SQL dialect is supported
Expand All @@ -206,15 +206,15 @@
from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
conn = st.experimental_connection("gsheets", type=GSheetsConnection)
conn = st.connection("gsheets", type=GSheetsConnection)

# click button to update worksheet
# This is behind a button to avoid exceeding Google API Quota
if st.button("Clear worksheet"):
conn.clear(worksheet="Example 1")
st.info("Worksheet Example 1 Cleared!")
st.cache_data.clear()
st.experimental_rerun()
st.rerun()

# click button to delete worksheet using the underlying gspread API
# This is behind a button to avoid exceeding Google API Quota
Expand All @@ -223,4 +223,4 @@
worksheet = spreadsheet.worksheet("Example 1")
spreadsheet.del_worksheet(worksheet)
st.cache_data.clear()
st.experimental_rerun()
st.rerun()
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[mypy]
ignore_missing_imports = True
ignore_missing_imports = True
26 changes: 25 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
[tool.ruff]
line-length=125
exclude = [".git", ".vscode", ".pytest_cache", ".mypy_cache", ".env"]
line-length = 125

[tool.ruff.lint]
ignore = ["B008", "ISC001", "E501", "W191"]
select = [
"B",
"E",
"F",
"W",
"I",
"N",
"C4",
"EXE",
"ISC",
"ICN",
"PIE",
"PT",
"RET",
"SIM",
"ERA",
"PLC",
"RUF",
"ARG",
]
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from pathlib import Path

import setuptools

VERSION = "0.0.4" # PEP-440
VERSION = "0.1.0" # PEP-440

NAME = "st-gsheets-connection"

INSTALL_REQUIRES = [
"streamlit>=1.22.0",
"streamlit>=1.32.0",
"gspread>=5.8.0, <6",
"gspread-pandas>=3.2.2",
"gspread-dataframe>=3.3.0",
"gspread-formatting>=1.1.2",
"pandas>=1.3.0, <2",
"duckdb>=0.8.1",
"sql-metadata>=2.7.0",
"validators>=0.22.0",
Expand Down Expand Up @@ -42,6 +43,6 @@
# Requirements
install_requires=INSTALL_REQUIRES,
packages=["streamlit_gsheets"],
long_description=open("README.md").read(),
long_description=Path("README.md").read_text(),
long_description_content_type="text/markdown",
)
Loading

0 comments on commit cd9a6ba

Please sign in to comment.