forked from sortitionfoundation/stratification-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
272 lines (233 loc) · 9.49 KB
/
script.py
File metadata and controls
272 lines (233 loc) · 9.49 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import platform
import sys
from io import StringIO
import eel
from stratification import (
PeopleAndCatsCSV,
PeopleAndCatsGoogleSheet,
NoSettingsFile,
Settings,
)
# to be honest this is no longer a file contents class - it's a GUI interface handler
# all the "content" has been moved into the PeopleAndCats class and its children
class FileContents():
def __init__(self):
self.PeopleAndCats = None
self._settings = None
# All of these below are only used in the Google Sheet version
self.g_sheet_name = ''
self.respondents_tab_name = 'Respondents' # Instance attribute for Advanced Settings
self.category_tab_name = 'Categories' # Instance attribute for Advanced Settings
self.gen_rem_tab = 'on' # Instance attribute for Advanced Settings
self.number_selections = 1 # Instance attribute for Advanced Settings (then later stored in PeopleAndCats)
@property
def settings(self):
self._init_settings()
return self._settings
def _init_settings(self):
"""
Call from lots of places to report the error early
"""
message = ""
if self._settings is None:
self._settings, message = Settings.load_from_file()
return message
def _add_category_content(self, input_content):
min_selection = 0
max_selection = 0
msg = []
try:
message = self._init_settings()
if message != "":
msg += [ message ]
except Exception as error:
self.PeopleAndCats.category_content_loaded = False
msg += [ "Error reading in settings file: {}".format(error) ]
try:
msg2, min_selection, max_selection = self.PeopleAndCats.load_cats( input_content, self.category_tab_name, self._settings )
msg += msg2
except gspread.exceptions.APIError:
msg3 = [ "Please wait a couple of seconds while gsheet updates. After waiting you may need to reload sheet." ]
msg += msg3
except Exception as error:
self.PeopleAndCats.category_content_loaded = False
msg += [ "Error reading in categories file: {}".format(error) ]
print(msg)
eel.update_categories_output_area("<br />".join(msg))
self.update_selection_content()
eel.update_selection_range(min_selection, max_selection)
# if these are the same just set the value!
if min_selection == max_selection and min_selection > 0:
eel.set_select_number_people(str(min_selection))
self.PeopleAndCats.number_people_to_select = int(min_selection)
# if we've already uploaded people, we need to re-process them with the
# (possibly) new categories settings
if self.PeopleAndCats.people_content_loaded:
dummy_file_contents=''
msg = self.PeopleAndCats.load_people(self.settings, dummy_file_contents, self.respondents_tab_name, self.category_tab_name, self.gen_rem_tab)
eel.update_selection_output_area("<br />".join(msg))
self.update_run_button()
# called from CSV input
def add_category_content(self, file_contents):
if file_contents != '':
self.PeopleAndCats = PeopleAndCatsCSV()
self._add_category_content( file_contents )
def _clear_messages(self, normal_message = "Number of categories: You must (re)load sheet..." ):
eel.update_categories_output_area( normal_message )
eel.update_selection_output_area( normal_message )
eel.update_selection_output_messages_area("")
eel.set_select_number_people('')
# called from g-sheet input
def update_g_sheet_name(self, g_sheet_name_input):
self._clear_messages()
self.g_sheet_name = g_sheet_name_input
if self.g_sheet_name != '':
eel.enable_load_g_sheet_btn()
# user has hit the (re)load button
# do cats and people at same time...
def load_g_sheet(self):
# this can happen if they enter something and then delete it...
if self.g_sheet_name == '':
self._clear_messages( "Please enter a spreadsheet name..." )
else:
self._clear_messages( "Requesting data from sheet..." )
try:
self.PeopleAndCats = PeopleAndCatsGoogleSheet()
# tell this object what this currently is...
self.PeopleAndCats.number_selections = self.number_selections
self._add_category_content( self.g_sheet_name )
dummy_file_contents=''
msg = self.PeopleAndCats.load_people(self.settings, dummy_file_contents, self.respondents_tab_name, self.category_tab_name, self.gen_rem_tab )
eel.update_selection_output_area("<br />".join(msg))
self.update_run_button()
eel.enable_load_g_sheet_btn()
except Exception as error:
eel.update_categories_output_area( "Please wait a couple of seconds while gsheet updates. After waiting you may need to reload sheet." )
###############################################################################
###The next functions read in extra instance variables for advanced settings###
###############################################################################
def update_respondents_tab_name(self, respondents_tab_name_input):
self._clear_messages()
self.respondents_tab_name = respondents_tab_name_input
def update_categories_tab_name(self, categories_tab_name_input):
self._clear_messages()
self.category_tab_name = categories_tab_name_input
def update_gen_rem_tab(self, gen_rem_tab_input):
self.gen_rem_tab = gen_rem_tab_input
# never generate a remaining tab if doing a multiple selection
if self.number_selections > 1:
self.gen_rem_tab = 'off'
def update_number_selections(self, number_selections_input):
self._clear_messages()
if number_selections_input == '':
self.number_selections = 1
else:
self.number_selections = int(number_selections_input)
# never generate a remaining tab if doing a multiple selection
if self.number_selections > 1:
self.gen_rem_tab = 'off'
########################################
###End of Advanced Settings variables###
########################################
### From here 'selection' means people...
def add_selection_content(self, file_contents):
self._init_settings()
# this calls update internally
msg = self.PeopleAndCats.load_people(self.settings, file_contents, self.respondents_tab_name, self.category_tab_name, self.gen_rem_tab)
eel.update_selection_output_area("<br />".join(msg))
self.update_run_button()
# 'selection' means people...
def update_selection_content(self):
if self.PeopleAndCats.category_content_loaded:
eel.enable_selection_content()
def update_run_button(self):
if self.PeopleAndCats.category_content_loaded and self.PeopleAndCats.people_content_loaded and self.PeopleAndCats.number_people_to_select > 0:
eel.enable_run_button()
else:
eel.disable_run_button()
if self.PeopleAndCats.number_people_to_select <= 0:
eel.set_select_number_people('')
def update_number_people(self, number_people):
if number_people == '':
self.PeopleAndCats.number_people_to_select = 0
else:
self.PeopleAndCats.number_people_to_select = int(number_people)
self.update_run_button()
def run_selection(self, test_selection ):
self._init_settings()
# they may have hit this button again, so clear the output area so it's more obvious
eel.update_selection_output_messages_area("Selecting... please wait...<br />")
success, output_lines = self.PeopleAndCats.people_cats_run_stratification( self.settings, test_selection )
if success and self.PeopleAndCats.get_selected_file() is not None and self.PeopleAndCats.get_remaining_file() is not None:
eel.enable_selected_download(self.PeopleAndCats.get_selected_file().getvalue(), 'selected.csv')
eel.enable_remaining_download(self.PeopleAndCats.get_remaining_file().getvalue(), 'remaining.csv')
# print output_lines to the App:
eel.update_selection_output_messages_area("<br />".join(output_lines))
# global to hold contents uploaded from JS
# not really - now just a GUI event handler more or less...
csv_files = FileContents()
@eel.expose
def handle_category_contents(file_contents):
csv_files.add_category_content(file_contents)
# 'selection' means people...
@eel.expose
def handle_selection_contents(file_contents):
csv_files.add_selection_content(file_contents)
@eel.expose
def update_g_sheet_name(g_sheet_name):
csv_files.update_g_sheet_name(g_sheet_name)
@eel.expose
def load_g_sheet():
csv_files.load_g_sheet()
#############################
###Start Advanced Settings###
#############################
@eel.expose
def update_respondents_tab_name(respondents_tab_name):
csv_files.update_respondents_tab_name(respondents_tab_name)
@eel.expose
def reload_respondents_tab():
csv_files.update_respondents_tab_name('')
@eel.expose
def update_categories_tab_name(categories_tab_name):
csv_files.update_categories_tab_name(categories_tab_name)
@eel.expose
def reload_categories_tab():
csv_files.update_categories_tab_name('')
@eel.expose
def update_gen_rem_tab(gen_rem_tab):
csv_files.update_gen_rem_tab(gen_rem_tab)
@eel.expose
def reload_gen_rem_tab():
csv_files.update_gen_rem_tab('')
@eel.expose
def update_number_selections(number_selections):
csv_files.update_number_selections(number_selections)
@eel.expose
def reload_number_selections():
csv_files.update_number_selections('')
###########################
###End Advanced Settings###
###########################
@eel.expose
def update_number_people(number_people):
csv_files.update_number_people(number_people)
@eel.expose
def run_selection():
csv_files.run_selection( False )
@eel.expose
def run_test_selection():
csv_files.run_selection( True )
def main():
default_size = (800, 800)
eel.init('web') # Give folder containing web files
try:
eel.start('main.html', size=default_size)
except EnvironmentError:
# on Windows 10 try Edge if Chrome not available
if sys.platform in ('win32', 'win64') and int(platform.release()) >= 10:
eel.start('main.html', mode='edge', size=default_size)
else:
raise
if __name__ == '__main__':
main()