forked from streamlit/example-app-bug-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (87 loc) · 3.06 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import google_auth_httplib2
import httplib2
import pandas as pd
import streamlit as st
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import HttpRequest
SCOPE = "https://www.googleapis.com/auth/spreadsheets"
SPREADSHEET_ID = "1QlPTiVvfRM82snGN6LELpNkOwVI1_Mp9J9xeJe-QoaA"
SHEET_NAME = "Database"
GSHEET_URL = f"https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}"
@st.experimental_singleton()
def connect_to_gsheet():
# Create a connection object.
credentials = service_account.Credentials.from_service_account_info(
st.secrets["gcp_service_account"],
scopes=[SCOPE],
)
# Create a new Http() object for every request
def build_request(http, *args, **kwargs):
new_http = google_auth_httplib2.AuthorizedHttp(
credentials, http=httplib2.Http()
)
return HttpRequest(new_http, *args, **kwargs)
authorized_http = google_auth_httplib2.AuthorizedHttp(
credentials, http=httplib2.Http()
)
service = build(
"sheets",
"v4",
requestBuilder=build_request,
http=authorized_http,
)
gsheet_connector = service.spreadsheets()
return gsheet_connector
def get_data(gsheet_connector) -> pd.DataFrame:
values = (
gsheet_connector.values()
.get(
spreadsheetId=SPREADSHEET_ID,
range=f"{SHEET_NAME}!A:E",
)
.execute()
)
df = pd.DataFrame(values["values"])
df.columns = df.iloc[0]
df = df[1:]
return df
def add_row_to_gsheet(gsheet_connector, row) -> None:
gsheet_connector.values().append(
spreadsheetId=SPREADSHEET_ID,
range=f"{SHEET_NAME}!A:E",
body=dict(values=row),
valueInputOption="USER_ENTERED",
).execute()
st.set_page_config(page_title="Bug report", page_icon="🐞", layout="centered")
st.title("🐞 Bug report!")
gsheet_connector = connect_to_gsheet()
st.sidebar.write(
f"This app shows how a Streamlit app can interact easily with a [Google Sheet]({GSHEET_URL}) to read or store data."
)
st.sidebar.write(
f"[Read more](https://docs.streamlit.io/knowledge-base/tutorials/databases/public-gsheet) about connecting your Streamlit app to Google Sheets."
)
form = st.form(key="annotation")
with form:
cols = st.columns((1, 1))
author = cols[0].text_input("Report author:")
bug_type = cols[1].selectbox(
"Bug type:", ["Front-end", "Back-end", "Data related", "404"], index=2
)
comment = st.text_area("Comment:")
cols = st.columns(2)
date = cols[0].date_input("Bug date occurrence:")
bug_severity = cols[1].slider("Bug severity:", 1, 5, 2)
submitted = st.form_submit_button(label="Submit")
if submitted:
add_row_to_gsheet(
gsheet_connector,
[[author, bug_type, comment, str(date), bug_severity]],
)
st.success("Thanks! Your bug was recorded.")
st.balloons()
expander = st.expander("See all records")
with expander:
st.write(f"Open original [Google Sheet]({GSHEET_URL})")
st.dataframe(get_data(gsheet_connector))