Skip to content

Commit d05a452

Browse files
committed
#454 Added sending data to googlesheet
#454 Added sending data to googlesheet
1 parent af1219a commit d05a452

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/penn_chime/presentation.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,9 @@ def display_sidebar(st, d: Parameters) -> Parameters:
355355
max_y_axis = max_y_axis_input()
356356

357357
current_date = current_date_input()
358-
####ToDo
359358
#Subscribe implementation
360-
st_obj.subheader ("Subscribe")
361-
email = st_obj.text_input (label="Enter Email", value="", key="na_lower_1")
362-
name = st_obj.text_input (label="Enter Name", value="", key="na_upper_1")
363-
affiliation = st_obj.text_input (label="Enter Affiliation", value="", key="na_upper_2")
364-
if st_obj.button (label="Submit", key="ta_submit_1"):
365-
send_subscription_to_google_sheet (st_obj, email, name, affiliation)
359+
subscribe(st_obj)
366360

367-
#####ToDo
368361
return Parameters(
369362
current_hospitalized=current_hospitalized,
370363
hospitalized=Disposition(hospitalized_rate, hospitalized_days),
@@ -382,14 +375,18 @@ def display_sidebar(st, d: Parameters) -> Parameters:
382375
population=population,
383376
)
384377

385-
#ToDo
386-
def send_subscription_to_google_sheet(st_obj,email, name, affiliation):
387-
print ("send email:" + email + " name:" + name + " affiliation:" + affiliation + " to google sheet")
388-
spr = sp.spreadsheet (st_obj, 'penn_chime/client_secret.json')
389-
header = ["ContactEmail","Name","Affiliation"]
390-
sheet = spr.createsheet("SheetnameToDo", header)
391-
row = [email,name,affiliation]
392-
spr.writeToSheet(sheet, row)
378+
def subscribe(st_obj):
379+
st_obj.subheader ("Subscribe")
380+
email = st_obj.text_input (label="Enter Email", value="", key="na_lower_1")
381+
name = st_obj.text_input (label="Enter Name", value="", key="na_upper_1")
382+
affiliation = st_obj.text_input (label="Enter Affiliation", value="", key="na_upper_2")
383+
if st_obj.button (label="Submit", key="ta_submit_1"):
384+
row = [email, name, affiliation]
385+
send_subscription_to_google_sheet(st_obj, row)
386+
387+
def send_subscription_to_google_sheet(st_obj, row):
388+
spr = sp.spreadsheet (st_obj, 'client_secret.json')
389+
spr.writeToSheet("CHIME Form Submissions", row)
393390

394391
def write_definitions(st):
395392
st.subheader("Guidance on Selecting Inputs")
@@ -400,7 +397,6 @@ def write_definitions(st):
400397
)
401398
)
402399

403-
404400
def write_footer(st):
405401
st.subheader("References & Acknowledgements")
406402
st.markdown(

src/penn_chime/spreadsheet.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
from oauth2client.service_account import ServiceAccountCredentials
33

44
class spreadsheet:
5-
def __init__(self, st_obj, secret, kwargs):
5+
def __init__(self, st_obj, secret):
66
self.st_obj = st_obj
7-
self.kwargs = kwargs
87
# use creds to create a client to interact with the Google Drive API
9-
self.scope = ['https://spreadsheets.google.com/feeds']
8+
self.scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
109
#secret = 'client_secret.json'
1110
self.creds = ServiceAccountCredentials.from_json_keyfile_name(secret, self.scope)
1211
self.client = gspread.authorize(self.creds)
@@ -26,12 +25,20 @@ def createsheet(self, sheetname, header):
2625
print("Caught exception " + e.__str__())
2726
return sheet
2827

29-
def writeToSheet(self, sheet, row):
28+
def writeToSheet(self, sheetname, row):
3029
try:
30+
sheet = self.client.open (sheetname).sheet1
31+
list_of_hashes = sheet.get_all_records ( )
3132
currentrow = sheet.row_count
3233
sheet.insert_row (row, currentrow)
3334
except Exception as e:
3435
print("Caught exception " + e.__str__())
3536

36-
37-
37+
def getAllRows(self, sheetname):
38+
allrows = None
39+
try:
40+
sheet = self.client.open (sheetname).sheet1
41+
allrows = sheet.get_all_records ( )
42+
except Exception as e:
43+
print("Caught exception " + e.__str__())
44+
return allrows

0 commit comments

Comments
 (0)