File size: 12,137 Bytes
01be203
601fb97
51bebb7
fb74711
202e927
 
 
601fb97
7a8c5bd
f7a0352
e8bc8aa
b7ef820
 
202e927
5a95626
84774df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ee4ee9
 
 
 
 
 
 
 
 
 
84774df
 
6ee4ee9
c8676bb
c583746
51bebb7
 
 
 
 
f3f92ee
51bebb7
9abcbf2
f3f92ee
00989c0
8b5eb21
84774df
00989c0
51bebb7
 
0862399
51bebb7
 
 
dc3a359
d92c6e9
 
 
7a8c5bd
 
 
 
 
 
108cecc
0862399
84774df
 
bb44945
bee8dc3
84774df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108cecc
84774df
6ee4ee9
 
 
 
 
 
 
 
 
31bd471
51bebb7
c8676bb
31bd471
 
 
8196a59
84774df
8196a59
e07dc97
e4680fa
601fb97
202e927
 
6ee4ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4fa10c
202e927
9eec8db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7ef820
 
 
 
 
 
fbe6e8c
b7ef820
13a1013
6ee4ee9
13a1013
84774df
13a1013
e4680fa
51bebb7
84774df
51bebb7
fb74711
51bebb7
84774df
 
e4680fa
84774df
bb44945
cd6e8ee
bb44945
575a625
bc0f5f9
 
 
51bebb7
dc3a359
202e927
a42d465
84774df
 
72cb3c7
 
84774df
91ee80c
b7ef820
52fb515
22f712c
6e2f4d4
22f712c
 
 
 
 
b7ef820
7d016cf
84774df
 
 
 
 
 
 
 
 
 
 
 
6ee4ee9
601fb97
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import gradio as gr
import requests
from urllib.parse import urljoin, urlencode, quote
from gradio_toggle import Toggle
import json
import yaml

#from process_data import parse_survey_stack_parameters, parse_survey_stack_data, process_specifications
from process_data import parse_survey_stack, process_specifications

from script_for_automation import drive_process

global original_outputs, yaml_outputs

#def process_survey(button_type, survey_submission_ID): 
#    """
#    Gets the data from the survey submission, then processes it with models to get output (data-filled) JSON Schemas. 
#    parse_survey_stack_data and parse_survey_stack_parameters complete the "back-end" data processing and can be found in a separate file
#
#    Args: 
#        button_type: (str) 'data' or (str) 'param' indicating whether processing the data survey or the parameter survey. When the parameter survey is processed, it sends the data for further processing.
#        survey_submission_ID: (str) Survey Submission ID (for retrieving survey)
#
#    Returns: 
#        (1) All front-end interactivity for hiding already used elements and showing new ones
#        (2) when button-type == 'data', the parsed input data
#        (3) when button-type == 'param', 3 JSONs representing the schemas for farm activity, interactions, and scientific trials
#    """
#    if button_type == 'data':
#        survey_id = "671fc52152e3225d7de349e6"
#        
#    else: 
#        survey_id = "671fe2d352e3225d7de34afc"
#        
#    base_url = "https://app.surveystack.io/api/submissions"
#    params = {
#        "survey": survey_id,
#    }
#
#    encoded_params = urlencode(params) 
#
#    match_param = f'{{"_id": {{"$oid": "{survey_submission_ID}"}}}}'
#
#    survey_url = f"{base_url}?{encoded_params}&match={match_param}"
#
#    print("Button Type: " + button_type)
#    print("PROCESS_SURVEY")
#    print(survey_url)
#
#    try:
#        global original_outputs, yaml_outputs
#        response = requests.get(survey_url)
#        response.raise_for_status()
#        data = response.json()
#
#       print("DATA FROM SURVEY")
#        print(data)
#
#        if button_type == 'data': 
#            global parsed_input_data
#            parsed_input_data = parse_survey_stack_data(data)
#            return parsed_input_data, gr.HTML(visible=True), gr.Textbox(visible=False), gr.Button(visible=False), gr.Textbox(visible=True), gr.Button(visible=True)
#        else:
#            parsed_param_data = parse_survey_stack_parameters(data)
#            parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_input_data, parsed_param_data)
#            original_outputs = [parsed_farm_json, parsed_interactions_json, parsed_trial_json]
#            yaml_outputs = []
#            return parsed_farm_json, parsed_interactions_json, parsed_trial_json, gr.Textbox(visible=True), gr.Textbox(visible=True), gr.Textbox(visible=True), Toggle(visible=True)
#        
#    except Exception as e:
#        return f"An error occurred while fetching the survey data: {e}"

def process_new_survey(survey_submission_ID): 
    """
    Gets the data from the survey submission, then processes it with models to get output (data-filled) JSON Schemas. 
    parse_survey_stack_data and parse_survey_stack_parameters complete the "back-end" data processing and can be found in a separate file

    Args: 
        button_type: (str) 'data' or (str) 'param' indicating whether processing the data survey or the parameter survey. When the parameter survey is processed, it sends the data for further processing.
        survey_submission_ID: (str) Survey Submission ID (for retrieving survey)

    Returns: 
        (1) All front-end interactivity for hiding already used elements and showing new ones
        (2) the parsed input data
        (3) 3 JSONs representing the schemas for farm activity, interactions, and scientific trials
    """
    survey_id = "673b4994aef86f0533b3546c"
        
    base_url = "https://app.surveystack.io/api/submissions"
    params = {
        "survey": survey_id,
    }

    encoded_params = urlencode(params) 

    match_param = f'{{"_id": {{"$oid": "{survey_submission_ID}"}}}}'

    survey_url = f"{base_url}?{encoded_params}&match={match_param}"

    print("PROCESSING_SURVEY")
    print(survey_url)

    try:
        global original_outputs, yaml_outputs
        response = requests.get(survey_url)
        response.raise_for_status()
        data = response.json()

        print("DATA FROM SURVEY")
        print(data)

        #parsed_input_data = parse_survey_stack_data(data)
        #parsed_param_data = parse_survey_stack_parameters(data)

        parsed_inputs = parse_survey_stack(data)

        print("THATS ALL FOLKS")

        parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_inputs)
        original_outputs = [parsed_farm_json, parsed_interactions_json, parsed_trial_json]
        yaml_outputs = []

        return parsed_inputs, parsed_farm_json, parsed_interactions_json, parsed_trial_json, gr.Textbox(visible=True), gr.Textbox(visible=True), gr.Textbox(visible=True), Toggle(visible=True)
    except Exception as e:
        return f"An error occurred while fetching the survey data: {e}"
        
#def display_parameter_survey():
#    """
#    Display parameter survey based on the IDs necessary to render the survey
#
#    Args: 
#        None 
#
#    Returns: 
#        HTML of survey in an iframe which gradio renders automagically in the variable parameter_survey_output
#    """
#    base_url = "https://app.surveystack.io/groups/"
#    group_id = "5e95e368fbbf75000146a006"  # Group ID "Our-Sci"
#    survey_id = "671fe2d352e3225d7de34afc" # survey id for parameter survey
#
#    # Construct the full URL using urljoin to ensure proper formatting
#    survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
#
#    print("PARAMETER_SURVEY")
#    print(survey_url)
#
#    
#    iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
#    
#    return iframe_html
#    
#def display_data_survey():
#    """
#    Display data survey based on the IDs necessary to render the survey
#
#    Args: 
#        None 
#
#    Returns: 
#        HTML of survey in an iframe which gradio renders automagically in the variable data_survey_output
#    """
#    base_url = "https://app.surveystack.io/groups/"
#    group_id = "5e95e368fbbf75000146a006"  # Group ID "Our-Sci"
#    survey_id = "671fc52152e3225d7de349e6" # survey id for data survey
#
#    # Construct the full URL using urljoin to ensure proper formatting
#    survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
#
#    print("DATA_SURVEY")
#    print(survey_url)
#    
#    iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
#    return iframe_html

def display_new_survey():
    """
    Display data survey based on the IDs necessary to render the survey

    Args: 
        None 

    Returns: 
        HTML of survey in an iframe which gradio renders automagically in the variable data_survey_output
    """
    base_url = "https://app.surveystack.io/groups/"
    group_id = "5e95e368fbbf75000146a006"  # Group ID "Our-Sci"
    survey_id = "673b4994aef86f0533b3546c" # survey id 

    # Construct the full URL using urljoin to ensure proper formatting
    survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")

    print("SURVEY SURVEY")
    print(survey_url)
    
    iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
    return iframe_html
    
def update_toggle(toggle, farm_output_box, interactions_output_box, trials_output_box):
    """
    Used as function for toggle (button that you can slide left and right). 
    Allows for data to be transformed between JSON and YAML forms, without losing either
    Both are persisted as globals (not a great practice in general) but allows for swapping between for viewing
    Toggle is not a part of base gradio and it was added additionally from here https://github.com/dwancin/gradio-toggle
    
    Args: 
        toggle: the Toggle element 
        farm_output_box: the text box designated for displaying the farm activity data JSON
        interactions_output_box: the text box designated for displaying the interactions data JSON
        trials_output_box: the text box designated for displaying the trials data JSON

    Returns: 
        3 YAML/JSON outputs displayed in the appropriate textboxes 
    """
    global original_outputs, yaml_outputs
    
    if toggle and not yaml_outputs:
        farm_dict = json.loads(farm_output_box)
        interactions_dict = json.loads(interactions_output_box)
        trials_dict = json.loads(trials_output_box)

        farm_yaml = yaml.dump(farm_dict)
        interactions_yaml = yaml.dump(interactions_dict)
        trials_yaml = yaml.dump(trials_dict)
        
        yaml_outputs = [farm_yaml, interactions_yaml, trials_yaml]
        
        return farm_yaml, interactions_yaml, trials_yaml
    elif toggle and yaml_outputs:
        return yaml_outputs[0], yaml_outputs[1], yaml_outputs[2]
    else:
        return original_outputs[0], original_outputs[1], original_outputs[2]

def generate_output_and_download():
    zip_file_path = drive_process()
    if os.path.exists(zip_file_path):
        return zip_file_path
    else: 
        return "Error: Output Folder not generated"
        
with gr.Blocks() as app: 
    # This is the main driver of the program that sets up the gradio front end pieces, as well as specific interactivity with buttons
    with gr.Row():
        survey_output = gr.HTML(value=display_new_survey())

    survey_submission_ID = gr.Textbox(label="Before submitting your surveystack survey, PLEASE record your submission ID. Type it here.")
    
    #data_submit_button = gr.Button("Parse Data")

    get_survey_back = gr.Textbox(label="Data from SurveyStack")

    #with gr.Row():
    #    parameter_survey_output = gr.HTML(value=display_parameter_survey(), visible=False)
            
    #survey2_submission_ID = gr.Textbox(label="Before submitting your surveystack survey, PLEASE record your submission ID. Type it here.", visible=False)

    submit_button = gr.Button("Create JSONs")

    with gr.Row():
        json_output_farm = gr.Textbox(label="Farm Data JSON", visible=False)
        json_output_interactions = gr.Textbox(label="Interactions Data JSON", visible=False)
        json_output_trials = gr.Textbox(label="Trials Data JSON", visible=False)


    toggle_output = Toggle(label="JSON <-> YAML", value=False, info="Toggle Output Data", interactive=True, visible=False)
    toggle_output.change(fn=update_toggle, inputs=[toggle_output, json_output_farm, json_output_interactions, json_output_trials], outputs=[json_output_farm, json_output_interactions, json_output_trials])

    submit_button.click(
        fn=process_new_survey,
        inputs=survey_submission_ID,
        outputs=[get_survey_back, json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials, toggle_output]
    )

    
    generate_output_button = gr.Button("Generate Output Folder and Download")
     
    generate_output_button.click(
        fn=generate_output_and_download,
        inputs=[],
        outputs=[gr.File(label="Download Output Folder")]
    )

    
    #param_submit_button.click(
    #    lambda button_type: process_survey("param", button_type),  
    #    inputs=survey2_submission_ID, 
    #    outputs=[json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials, toggle_output]
    #)
    
    #data_submit_button.click(
    #    lambda button_type: process_survey("data", button_type), 
    #    inputs=survey_submission_ID, 
    #    outputs=[get_survey_back, parameter_survey_output, data_survey_output, data_submit_button, survey2_submission_ID, param_submit_button]
    #)
    
# This starts the program
app.launch()