-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sfc-gh-zblackwood/linting
Linting
- Loading branch information
Showing
10 changed files
with
319 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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)) | ||
|
@@ -145,6 +184,7 @@ | |
|
||
with st.echo(): | ||
import streamlit as st | ||
|
||
from streamlit_gsheets import GSheetsConnection | ||
|
||
# Create GSheets connection | ||
|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[mypy] | ||
ignore_missing_imports = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tool.ruff] | ||
line-length=125 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.