Skip to content

Commit af1219a

Browse files
committed
#454 created spreadsheet handler
#454 created spreadsheet handler
1 parent 6a4211b commit af1219a

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/penn_chime/presentation.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import altair as alt
77
import numpy as np
88
import pandas as pd
9-
9+
import penn_chime.spreadsheet as sp
1010
from .constants import (
1111
CHANGE_DATE,
1212
DATE_FORMAT,
@@ -355,14 +355,16 @@ def display_sidebar(st, d: Parameters) -> Parameters:
355355
max_y_axis = max_y_axis_input()
356356

357357
current_date = current_date_input()
358+
####ToDo
358359
#Subscribe implementation
359360
st_obj.subheader ("Subscribe")
360361
email = st_obj.text_input (label="Enter Email", value="", key="na_lower_1")
361362
name = st_obj.text_input (label="Enter Name", value="", key="na_upper_1")
362363
affiliation = st_obj.text_input (label="Enter Affiliation", value="", key="na_upper_2")
363364
if st_obj.button (label="Submit", key="ta_submit_1"):
364-
send_subscription_to_google_sheet (email, name, affiliation)
365+
send_subscription_to_google_sheet (st_obj, email, name, affiliation)
365366

367+
#####ToDo
366368
return Parameters(
367369
current_hospitalized=current_hospitalized,
368370
hospitalized=Disposition(hospitalized_rate, hospitalized_days),
@@ -380,9 +382,14 @@ def display_sidebar(st, d: Parameters) -> Parameters:
380382
population=population,
381383
)
382384

383-
def send_subscription_to_google_sheet(email, name, affiliation):
385+
#ToDo
386+
def send_subscription_to_google_sheet(st_obj,email, name, affiliation):
384387
print ("send email:" + email + " name:" + name + " affiliation:" + affiliation + " to google sheet")
385-
#implement sending the data to googlesheet
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)
386393

387394
def write_definitions(st):
388395
st.subheader("Guidance on Selecting Inputs")

src/penn_chime/spreadsheet.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import gspread
2+
from oauth2client.service_account import ServiceAccountCredentials
3+
4+
class spreadsheet:
5+
def __init__(self, st_obj, secret, kwargs):
6+
self.st_obj = st_obj
7+
self.kwargs = kwargs
8+
# use creds to create a client to interact with the Google Drive API
9+
self.scope = ['https://spreadsheets.google.com/feeds']
10+
#secret = 'client_secret.json'
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, sheet, row):
30+
try:
31+
currentrow = sheet.row_count
32+
sheet.insert_row (row, currentrow)
33+
except Exception as e:
34+
print("Caught exception " + e.__str__())
35+
36+
37+

0 commit comments

Comments
 (0)