Skip to content

Commit

Permalink
Merge pull request #6 from sfc-gh-zblackwood/linting
Browse files Browse the repository at this point in the history
Linting
  • Loading branch information
sfc-gh-jcarroll authored Jul 31, 2023
2 parents 01a419c + 7aea9f3 commit 090e94e
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 106 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/lint_and_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Python package

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
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 --format=github --select=E9,F63,F7,F82 --target-version=py37 .
# default set of ruff rules with GitHub Annotations
ruff --format=github --target-version=py37 .
- name: Check types with mypy
run: |
mypy --ignore-missing-imports .
- name: Test with pytest
run: |
pytest
6 changes: 3 additions & 3 deletions examples/st_app.py → examples/Public_Sheet_Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

conn = st.experimental_connection("gsheets", type=GSheetsConnection)
Expand All @@ -30,11 +31,10 @@

with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

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

df = conn.query(
'select births from "Example 2" limit 10', spreadsheet=url, usecols=[0, 1]
)
df = conn.query('select births from "Example 2" limit 10', spreadsheet=url)
st.dataframe(df)
Original file line number Diff line number Diff line change
@@ -1,60 +1,88 @@
import streamlit as st
import pandasql as psql
import streamlit as st

st.subheader("📗 Google Sheets st.connection using Service Account")

st.write("#### 1. API Reference")
with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

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

docs_url = (
"https://docs.streamlit.io/streamlit-community-cloud/get-started/"
"deploy-an-app/connect-to-data-sources/secrets-management"
)

st.write("#### 2. Initial setup")
st.markdown(
"""
f"""
## Initial setup for CRUD mode
1. Setup `.streamlit/secrets.toml` inside your Streamlit app root directory,
check out [Secret management documentation](https://docs.streamlit.io/streamlit-community-cloud/get-started/deploy-an-app/connect-to-data-sources/secrets-management) for references.
2. [Enable API Access for a Project](https://docs.gspread.org/en/v5.7.1/oauth2.html#enable-api-access-for-a-project)
* Head to [Google Developers Console](https://console.developers.google.com/) 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](https://docs.gspread.org/en/v5.7.1/oauth2.html#for-bots-using-service-account)
1. Setup `.streamlit/secrets.toml` inside your Streamlit app root directory,
check out [Secret management documentation]({docs_url}) for references.
2. [Enable API Access for a
Project](https://docs.gspread.org/en/v5.7.1/oauth2.html#enable-api-access-for-a-project)
* Head to [Google Developers Console](https://console.developers.google.com/) 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](https://docs.gspread.org/en/v5.7.1/oauth2.html#for-bots-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”.
* 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”.
* 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",
"private_key": "-----BEGIN PRIVATE KEY-----\\nNrDyLw … jINQh/9\\n-----END PRIVATE KEY-----\\n",
"client_email": "[email protected]",
"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.
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.
4. Inside `streamlit/secrets.toml` place `service_account` configuration from downloaded
JSON file, in the following format (where `gsheets` is your `st.connection` name):
4. 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"
# worksheet GID is used when using Public Spreadsheet URL, when using service_account
# it will be picked as folder_id
worksheet = "<worksheet-gid-or-folder-id>"
# leave empty when using Public Spreadsheet URL, when using
# service_account -> type = "service_account"
type = ""
project_id = ""
private_key_id = ""
private_key = ""
Expand All @@ -65,13 +93,15 @@
auth_provider_x509_cert_url = ""
client_x509_cert_url = ""
```
"""
)

st.write("#### 3. Load DataFrame into Google Sheets")

with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
Expand All @@ -80,29 +110,37 @@
# Demo Births DataFrame
df = psql.load_births()

# set create_spreadsheet to True to create spreadsheet,
# create_spreadsheet is False by default to avoid exceeding Google API Quota
create_spreadsheet = False

if create_spreadsheet:
# click button to update worksheet
# This is behind a button to avoid exceeding Google API Quota
if st.button("Create new worksheet"):
df = conn.create(
worksheet="Example 1",
data=df,
)
st.cache_data.clear()
st.experimental_rerun()

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


st.write("#### 4. Read Google WorkSheet as DataFrame")
st.info(
"If the sheet has been deleted, press 'Create new worksheet' button above.",
icon="ℹ️",
)

with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

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

# Read Google WorkSheet as DataFrame
df = conn.read(
worksheet="Example 1",
usecols=[
0,
1,
Expand All @@ -115,6 +153,7 @@
st.write("#### 5. Update Google WorkSheet using DataFrame")
with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
Expand All @@ -123,15 +162,15 @@
# Demo Meat DataFrame
df = psql.load_meat()

# set update_spreadsheet to True to update spreadsheet,
# update_spreadsheet is False by default to avoid exceeding Google API Quota
update_spreadsheet = False

if update_spreadsheet:
# click button to update worksheet
# This is behind a button to avoid exceeding Google API Quota
if st.button("Update worksheet"):
df = conn.update(
worksheet="Example 1",
data=df,
)
st.cache_data.clear()
st.experimental_rerun()

# Display our Spreadsheet as st.dataframe
st.dataframe(df.head(10))
Expand All @@ -145,6 +184,7 @@

with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

# Create GSheets connection
Expand All @@ -162,17 +202,25 @@
st.write("#### 7. Clear/delete worksheet")
with st.echo():
import streamlit as st

from streamlit_gsheets import GSheetsConnection

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

# set clear_worksheet to True to update spreadsheet,
# clear_worksheet is False by default to avoid exceeding Google API Quota
clear_worksheet = False

if clear_worksheet:
# 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")
# Uncomment this to delete worksheet Example 1
# conn.delete(spreadsheet=spreadsheet, worksheet="Example 1")
st.info("Worksheet Example 1 Cleared!")
st.cache_data.clear()
st.experimental_rerun()

# click button to delete worksheet using the underlying gspread API
# This is behind a button to avoid exceeding Google API Quota
if st.button("Delete worksheet"):
spreadsheet = conn.client._open_spreadsheet() # type: ignore
worksheet = spreadsheet.worksheet("Example 1")
spreadsheet.del_worksheet(worksheet)
st.cache_data.clear()
st.experimental_rerun()
4 changes: 2 additions & 2 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
streamlit==1.22
git+https://github.com/streamlit/gsheets-connection
streamlit>=1.22.0
pandasql # this is for examples/ only
git+https://github.com/streamlit/gsheets-connection
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
ignore_missing_imports = True
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.ruff]
line-length=125
2 changes: 1 addition & 1 deletion streamlit_gsheets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from streamlit_gsheets.gsheets_connection import GSheetsConnection
from streamlit_gsheets.gsheets_connection import GSheetsConnection # noqa
Loading

0 comments on commit 090e94e

Please sign in to comment.