-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapp.py
107 lines (84 loc) · 3.02 KB
/
myapp.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
# interface for gsheet ui for demo app 'myapp'
# -----------------------------------------------------
# Import
# -----------------------------------------------------
import datetime as dt
import pandas as pd
#import database as db
from sqlgsheet import database as db
# -----------------------------------------------------
# Module variables
# -----------------------------------------------------
# constants
UI_SHEET = 'myapp'
# dynamic
UI_CONFIG = {}
TABLES = {}
# -----------------------------------------------------
# Setup
# -----------------------------------------------------
def load():
''' loads basic configuration information for the module
'''
# loads the module interface to gsheet
db.load()
# load configuration information
load_config()
#load tables from gsheet
load_gsheets()
def load_config():
global UI_CONFIG
config_tbl = db.get_sheet(UI_SHEET, 'config')
UI_CONFIG = get_reporting_config(config_tbl)
def load_gsheets():
global TABLES
tables_config = db.GSHEET_CONFIG[UI_SHEET]['sheets'].copy()
for t in tables_config:
TABLES[t] = {}
TABLES[t]['gsheet'] = db.get_sheet(UI_SHEET, t)
def get_reporting_config(tbl):
DATE_FORMAT = '%Y-%m-%d'
config = {}
groups = list(tbl['group'].unique())
for grp in groups:
group_tbl = tbl[tbl['group'] == grp][['parameter', 'value', 'data_type']]
params = dict(group_tbl[['parameter', 'value']]
.set_index('parameter')['value'])
data_types = dict(group_tbl[['parameter', 'data_type']]
.set_index('parameter')['data_type'])
for p in params:
data_type = data_types[p]
if ~(data_type == 'str'):
if ~pd.isnull(params[p]):
if data_type in db.NUMERIC_TYPES:
numStr = params[p]
if numStr == '':
numStr = '0'
if data_type == 'int':
params[p] = int(numStr)
elif data_type == 'float':
params[p] = float(numStr)
elif data_type == 'date':
params[p] = dt.datetime.strptime(params[p], DATE_FORMAT).date()
config[grp] = params
return config
# -----------------------------------------------------
# Main
# -----------------------------------------------------
def update():
# 01 load tables
load()
# 02 get new form responses
form_responses = TABLES['form']['gsheet'].copy()
records = TABLES['records']['gsheet'].copy()
# 03 append new form responses to records and post to gsheet
records = records.append(form_responses)
db.post_to_gsheet(records,
UI_SHEET,
'records',
input_option='USER_ENTERED')
if __name__ == "__main__":
update()
# -----------------------------------------------------
# Reference code
# -----------------------------------------------------