Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
from urllib.parse import urljoin, urlencode, quote
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
from process_data import parse_survey_stack_parameters, parse_survey_stack_data, process_specifications
|
| 6 |
|
| 7 |
global parsed_input_data
|
|
|
|
| 8 |
|
| 9 |
def process_survey(button_type, submission_id):
|
| 10 |
if button_type == 'data':
|
|
@@ -32,7 +36,6 @@ def process_survey(button_type, submission_id):
|
|
| 32 |
response = requests.get(survey_url)
|
| 33 |
response.raise_for_status()
|
| 34 |
data = response.json()
|
| 35 |
-
#actual_data = data[0]['data']
|
| 36 |
|
| 37 |
print("DATA FROM SURVEY")
|
| 38 |
print(data)
|
|
@@ -44,7 +47,7 @@ def process_survey(button_type, submission_id):
|
|
| 44 |
else:
|
| 45 |
parsed_param_data = parse_survey_stack_parameters(data)
|
| 46 |
parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_input_data, parsed_param_data)
|
| 47 |
-
return parsed_farm_json, parsed_interactions_json, parsed_trial_json, gr.
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
return f"An error occurred while fetching the survey data: {e}"
|
|
@@ -76,7 +79,27 @@ def display_data_survey():
|
|
| 76 |
|
| 77 |
iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
| 78 |
return iframe_html
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
with gr.Blocks() as app:
|
| 81 |
with gr.Row():
|
| 82 |
data_survey_output = gr.HTML(value=display_data_survey())
|
|
@@ -102,13 +125,15 @@ with gr.Blocks() as app:
|
|
| 102 |
json_output_trials = gr.TextBox(label="Trials Data JSON", visible=False)
|
| 103 |
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
param_submit_button.click(
|
| 107 |
lambda button_type: process_survey("param", button_type),
|
| 108 |
inputs=survey2_submission_ID,
|
| 109 |
-
outputs=[json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials]
|
| 110 |
)
|
| 111 |
-
|
| 112 |
data_submit_button.click(
|
| 113 |
lambda button_type: process_survey("data", button_type),
|
| 114 |
inputs=[survey_submission_ID],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
from urllib.parse import urljoin, urlencode, quote
|
| 4 |
+
from gradio_toggle import Toggle
|
| 5 |
+
import json
|
| 6 |
+
import yaml
|
| 7 |
|
| 8 |
from process_data import parse_survey_stack_parameters, parse_survey_stack_data, process_specifications
|
| 9 |
|
| 10 |
global parsed_input_data
|
| 11 |
+
global original_outputs, yaml_outputs
|
| 12 |
|
| 13 |
def process_survey(button_type, submission_id):
|
| 14 |
if button_type == 'data':
|
|
|
|
| 36 |
response = requests.get(survey_url)
|
| 37 |
response.raise_for_status()
|
| 38 |
data = response.json()
|
|
|
|
| 39 |
|
| 40 |
print("DATA FROM SURVEY")
|
| 41 |
print(data)
|
|
|
|
| 47 |
else:
|
| 48 |
parsed_param_data = parse_survey_stack_parameters(data)
|
| 49 |
parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_input_data, parsed_param_data)
|
| 50 |
+
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)
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
return f"An error occurred while fetching the survey data: {e}"
|
|
|
|
| 79 |
|
| 80 |
iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
| 81 |
return iframe_html
|
| 82 |
+
|
| 83 |
+
def update_toggle(toggle, farm_output_box, interactions_output_box, trials_output_box):
|
| 84 |
+
global original_outputs, yaml_outputs
|
| 85 |
+
|
| 86 |
+
if toggle and not yaml_outputs:
|
| 87 |
+
farm_dict = json.loads(farm_output_box)
|
| 88 |
+
interactions_dict = json.loads(interactions_output_box)
|
| 89 |
+
trials_dict = json.loads(trials_output_box)
|
| 90 |
+
|
| 91 |
+
farm_yaml = yaml.dump(farm_dict)
|
| 92 |
+
interactions_yaml = yaml.dump(interactions_dict)
|
| 93 |
+
trials_yaml = yaml.dump(trials_dict)
|
| 94 |
+
|
| 95 |
+
yaml_outputs = [farm_yaml, interactions_yaml, trials_yaml]
|
| 96 |
+
|
| 97 |
+
return farm_yaml, interactions_yaml, trials_yaml
|
| 98 |
+
elif toggle and yaml_outputs:
|
| 99 |
+
return yaml_outputs[0], yaml_outputs[1], yaml_outputs[2]
|
| 100 |
+
else:
|
| 101 |
+
return original_outputs[0], original_outputs[1], original_outputs[2]
|
| 102 |
+
|
| 103 |
with gr.Blocks() as app:
|
| 104 |
with gr.Row():
|
| 105 |
data_survey_output = gr.HTML(value=display_data_survey())
|
|
|
|
| 125 |
json_output_trials = gr.TextBox(label="Trials Data JSON", visible=False)
|
| 126 |
|
| 127 |
|
| 128 |
+
toggle_output = Toggle(label="JSON <-> YAML", value=False, info="Toggle Output Data", interactive=True, visible=False)
|
| 129 |
+
toggle_output.change(fn=update_toggle, inputs=[toggle_output, farm_output_box, interactions_output_box, trials_output_box], outputs=[farm_output_box, interactions_output_box, trials_output_box])
|
| 130 |
+
|
| 131 |
param_submit_button.click(
|
| 132 |
lambda button_type: process_survey("param", button_type),
|
| 133 |
inputs=survey2_submission_ID,
|
| 134 |
+
outputs=[json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials, toggle_output]
|
| 135 |
)
|
| 136 |
+
|
| 137 |
data_submit_button.click(
|
| 138 |
lambda button_type: process_survey("data", button_type),
|
| 139 |
inputs=[survey_submission_ID],
|