Skip to content

Commit 90998ad

Browse files
authored
Merge pull request #513 from mangalap123/develop
#454 Subscribe to updates field
2 parents f3b1555 + bfb47f3 commit 90998ad

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"pyyaml",
3434
"selenium",
3535
"streamlit",
36+
"gspread",
37+
"oauth2client"
3638
],
3739
classifiers=[
3840
"Programming Language :: Python :: 3",

src/penn_chime/presentation.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
import altair as alt
77
import numpy as np
8+
import os
9+
import json
810
import pandas as pd
9-
11+
import penn_chime.spreadsheet as sp
1012
from .constants import (
1113
CHANGE_DATE,
1214
DOCS_URL,
@@ -172,6 +174,8 @@ def display_sidebar(st, d: Parameters) -> Parameters:
172174
# it's kindof like ember or angular if you are familiar with those
173175

174176
st_obj = st.sidebar
177+
# used_widget_key = st.get_last_used_widget_key ( )
178+
175179
current_hospitalized_input = NumberInput(
176180
st_obj,
177181
"Currently hospitalized COVID-19 patients",
@@ -349,6 +353,8 @@ def display_sidebar(st, d: Parameters) -> Parameters:
349353
max_y_axis = max_y_axis_input()
350354

351355
current_date = current_date_input()
356+
#Subscribe implementation
357+
subscribe(st_obj)
352358

353359
return Parameters(
354360
current_hospitalized=current_hospitalized,
@@ -374,6 +380,66 @@ def display_sidebar(st, d: Parameters) -> Parameters:
374380
days=ventilated_days),
375381
)
376382

383+
#Read the environment variables and cteate json key object to use with ServiceAccountCredentials
384+
def readGoogleApiSecrets():
385+
client_secret = {}
386+
os.getenv
387+
type = os.getenv ('GAPI_CRED_TYPE').strip()
388+
print (type)
389+
client_secret['type'] = type,
390+
client_secret['project_id'] = os.getenv ('GAPI_CRED_PROJECT_ID'),
391+
client_secret['private_key_id'] = os.getenv ('GAPI_CRED_PRIVATE_KEY_ID'),
392+
client_secret['private_key'] = os.getenv ('GAPI_CRED_PRIVATE_KEY'),
393+
client_secret['client_email'] = os.getenv ('GAPI_CRED_CLIENT_EMAIL'),
394+
client_secret['client_id'] = os.getenv ('GAPI_CRED_CLIENT_ID'),
395+
client_secret['auth_uri'] = os.getenv ('GAPI_CRED_AUTH_URI'),
396+
client_secret['token_uri'] = os.getenv ('GAPI_CRED_TOKEN_URI'),
397+
client_secret['auth_provider_x509_cert_url'] = os.getenv ('GAPI_CRED_AUTH_PROVIDER_X509_CERT_URL'),
398+
client_secret['client_x509_cert_url'] = os.getenv ('GAPI_CRED_CLIENT_X509_CERT_URI'),
399+
json_data = json.dumps (client_secret)
400+
print(json_data)
401+
return json_data
402+
403+
def readGoogleApiSecretsDict():
404+
type = os.getenv ('GAPI_CRED_TYPE')
405+
project_id = os.getenv ('GAPI_CRED_PROJECT_ID')
406+
private_key_id = os.getenv ('GAPI_CRED_PRIVATE_KEY_ID')
407+
private_key = os.getenv ('GAPI_CRED_PRIVATE_KEY')
408+
client_email = os.getenv ('GAPI_CRED_CLIENT_EMAIL')
409+
client_id = os.getenv ('GAPI_CRED_CLIENT_ID')
410+
auth_uri = os.getenv ('GAPI_CRED_AUTH_URI')
411+
token_uri = os.getenv ('GAPI_CRED_TOKEN_URI')
412+
auth_provider_x509_cert_url = os.getenv ('GAPI_CRED_AUTH_PROVIDER_X509_CERT_URL')
413+
client_x509_cert_url = os.getenv ('GAPI_CRED_CLIENT_X509_CERT_URI')
414+
415+
secret = {
416+
'type' : type,
417+
'project_id' : project_id,
418+
'private_key_id' : private_key_id,
419+
'private_key':private_key,
420+
'client_email': client_email,
421+
'client_id': client_id,
422+
'auth_uri': auth_uri,
423+
'token_uri': token_uri,
424+
'auth_provider_x509_cert_url':auth_provider_x509_cert_url,
425+
'client_x509_cert_url':client_x509_cert_url
426+
}
427+
return secret
428+
429+
def subscribe(st_obj):
430+
st_obj.subheader ("Subscribe")
431+
email = st_obj.text_input (label="Enter Email", value="", key="na_lower_1")
432+
name = st_obj.text_input (label="Enter Name", value="", key="na_upper_1")
433+
affiliation = st_obj.text_input (label="Enter Affiliation", value="", key="na_upper_2")
434+
if st_obj.button (label="Submit", key="ta_submit_1"):
435+
row = [email, name, affiliation]
436+
send_subscription_to_google_sheet(st_obj, row)
437+
438+
def send_subscription_to_google_sheet(st_obj, row):
439+
json_secret = readGoogleApiSecretsDict()
440+
#print(json_secret)
441+
spr = sp.spreadsheet (st_obj, json_secret)
442+
spr.writeToSheet("CHIME Form Submissions", row)
377443

378444
def display_footer(st):
379445
st.subheader("References & Acknowledgements")

src/penn_chime/spreadsheet.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import gspread
2+
from oauth2client.service_account import ServiceAccountCredentials
3+
4+
class spreadsheet:
5+
def __init__(self, st_obj, secret):
6+
self.st_obj = st_obj
7+
# use creds to create a client to interact with the Google Drive API
8+
self.scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
9+
#secret = 'client_secret.json'
10+
self.creds = ServiceAccountCredentials.from_json(secret)
11+
#self.creds = ServiceAccountCredentials.from_json_keyfile_name(secret, self.scope)
12+
self.client = gspread.authorize(self.creds)
13+
14+
15+
def createsheet(self, sheetname, header):
16+
sheet = None
17+
try:
18+
self.client.create(sheetname)
19+
except Exception as e:
20+
print("Caught exception " + e.__str__())
21+
try:
22+
sheet = self.client.open (sheetname).sheet1
23+
index = 0
24+
sheet.insert_row (header, index)
25+
except Exception as e:
26+
print("Caught exception " + e.__str__())
27+
return sheet
28+
29+
def writeToSheet(self, sheetname, row):
30+
try:
31+
sheet = self.client.open (sheetname).sheet1
32+
list_of_hashes = sheet.get_all_records ( )
33+
currentrow = sheet.row_count
34+
sheet.insert_row (row, currentrow)
35+
except Exception as e:
36+
print("Caught exception " + e.__str__())
37+
38+
def getAllRows(self, sheetname):
39+
allrows = None
40+
try:
41+
sheet = self.client.open (sheetname).sheet1
42+
allrows = sheet.get_all_records ( )
43+
except Exception as e:
44+
print("Caught exception " + e.__str__())
45+
return allrows

0 commit comments

Comments
 (0)