-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
62 lines (44 loc) · 1.59 KB
/
handler.py
File metadata and controls
62 lines (44 loc) · 1.59 KB
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
import os
import json
try:
import unzip_requirements
except ImportError:
pass
from jinja2 import Environment, Template
import pandas as pd
import tabulator
def dataframe_table_filter(df, **kwargs):
"""A Jinja filter that turns a Pandas DataFrame into HTML.
Keyword arguments are passed to DataFrame.to_html.
The Pandas display option is dynamically set to allow full-width text in the cells.
"""
pd_display_max_colwidth_key = 'display.max_colwidth'
saved_max_colwidth = pd.get_option(pd_display_max_colwidth_key)
try:
pd.set_option(pd_display_max_colwidth_key, -1)
return df.to_html(**kwargs)
finally:
pd.set_option(pd_display_max_colwidth_key, saved_max_colwidth)
def series_table_filter(series):
return dataframe_table_filter(pd.DataFrame(series), classes=['table', 'table-sm'])
env = Environment()
env.filters['series_table'] = series_table_filter
with open(os.path.join(os.path.dirname(__file__), './results.html')) as f:
template = env.from_string(f.read())
GITHUB_REPO_URL = os.getenv('GITHUB_REPO_URL')
def tabulate(event, context):
return {
"statusCode": 200,
"headers": {
"content-type": "text/html"
},
"body": tabulation_html()
}
def tabulation_html():
gsheet_key = os.getenv('GOOGLE_SHEET_KEY')
gsheet_range = os.getenv('GOOGLE_SHEET_RANGE')
tabulation = tabulator.tabulate(gsheet_key, gsheet_range)
return template.render(
results=tabulation, github_repo_url=GITHUB_REPO_URL, title='Poll Results')
if __name__ == '__main__':
print(tabulation_html())