Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,65 @@ from process_data import parse_survey_stack_parameters, parse_survey_stack_data,
|
|
| 11 |
global parsed_input_data
|
| 12 |
global original_outputs, yaml_outputs
|
| 13 |
|
| 14 |
-
def process_survey(button_type, survey_submission_ID):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
Gets the data from the survey submission, then processes it with models to get output (data-filled) JSON Schemas.
|
| 17 |
parse_survey_stack_data and parse_survey_stack_parameters complete the "back-end" data processing and can be found in a separate file
|
|
@@ -22,14 +80,10 @@ def process_survey(button_type, survey_submission_ID):
|
|
| 22 |
|
| 23 |
Returns:
|
| 24 |
(1) All front-end interactivity for hiding already used elements and showing new ones
|
| 25 |
-
(2)
|
| 26 |
-
(3)
|
| 27 |
"""
|
| 28 |
-
|
| 29 |
-
survey_id = "671fc52152e3225d7de349e6"
|
| 30 |
-
|
| 31 |
-
else:
|
| 32 |
-
survey_id = "671fe2d352e3225d7de34afc"
|
| 33 |
|
| 34 |
base_url = "https://app.surveystack.io/api/submissions"
|
| 35 |
params = {
|
|
@@ -42,12 +96,11 @@ def process_survey(button_type, survey_submission_ID):
|
|
| 42 |
|
| 43 |
survey_url = f"{base_url}?{encoded_params}&match={match_param}"
|
| 44 |
|
| 45 |
-
print("
|
| 46 |
-
print("PROCESS_SURVEY")
|
| 47 |
print(survey_url)
|
| 48 |
|
| 49 |
try:
|
| 50 |
-
global original_outputs, yaml_outputs
|
| 51 |
response = requests.get(survey_url)
|
| 52 |
response.raise_for_status()
|
| 53 |
data = response.json()
|
|
@@ -55,47 +108,66 @@ def process_survey(button_type, survey_submission_ID):
|
|
| 55 |
print("DATA FROM SURVEY")
|
| 56 |
print(data)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
parsed_input_data = parse_survey_stack_data(data)
|
| 61 |
-
return parsed_input_data, gr.HTML(visible=True), gr.Textbox(visible=False), gr.Button(visible=False), gr.Textbox(visible=True), gr.Button(visible=True)
|
| 62 |
-
else:
|
| 63 |
-
parsed_param_data = parse_survey_stack_parameters(data)
|
| 64 |
-
parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_input_data, parsed_param_data)
|
| 65 |
-
original_outputs = [parsed_farm_json, parsed_interactions_json, parsed_trial_json]
|
| 66 |
-
yaml_outputs = []
|
| 67 |
-
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)
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
return f"An error occurred while fetching the survey data: {e}"
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
Display parameter survey based on the IDs necessary to render the survey
|
| 76 |
-
|
| 77 |
-
Args:
|
| 78 |
-
None
|
| 79 |
-
|
| 80 |
-
Returns:
|
| 81 |
-
HTML of survey in an iframe which gradio renders automagically in the variable parameter_survey_output
|
| 82 |
-
"""
|
| 83 |
-
base_url = "https://app.surveystack.io/groups/"
|
| 84 |
-
group_id = "5e95e368fbbf75000146a006" # Group ID "Our-Sci"
|
| 85 |
-
survey_id = "671fe2d352e3225d7de34afc" # survey id for parameter survey
|
| 86 |
-
|
| 87 |
-
# Construct the full URL using urljoin to ensure proper formatting
|
| 88 |
-
survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
| 95 |
-
|
| 96 |
-
return iframe_html
|
| 97 |
-
|
| 98 |
-
def display_data_survey():
|
| 99 |
"""
|
| 100 |
Display data survey based on the IDs necessary to render the survey
|
| 101 |
|
|
@@ -107,12 +179,12 @@ def display_data_survey():
|
|
| 107 |
"""
|
| 108 |
base_url = "https://app.surveystack.io/groups/"
|
| 109 |
group_id = "5e95e368fbbf75000146a006" # Group ID "Our-Sci"
|
| 110 |
-
survey_id = "
|
| 111 |
|
| 112 |
# Construct the full URL using urljoin to ensure proper formatting
|
| 113 |
survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
|
| 114 |
|
| 115 |
-
print("
|
| 116 |
print(survey_url)
|
| 117 |
|
| 118 |
iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
|
@@ -156,20 +228,20 @@ def update_toggle(toggle, farm_output_box, interactions_output_box, trials_outpu
|
|
| 156 |
with gr.Blocks() as app:
|
| 157 |
# This is the main driver of the program that sets up the gradio front end pieces, as well as specific interactivity with buttons
|
| 158 |
with gr.Row():
|
| 159 |
-
|
| 160 |
|
| 161 |
survey_submission_ID = gr.Textbox(label="Before submitting your surveystack survey, PLEASE record your submission ID. Type it here.")
|
| 162 |
|
| 163 |
-
data_submit_button = gr.Button("Parse Data")
|
| 164 |
|
| 165 |
get_survey_back = gr.Textbox(label="Data from SurveyStack")
|
| 166 |
|
| 167 |
-
with gr.Row():
|
| 168 |
-
|
| 169 |
|
| 170 |
-
survey2_submission_ID = gr.Textbox(label="Before submitting your surveystack survey, PLEASE record your submission ID. Type it here.", visible=False)
|
| 171 |
|
| 172 |
-
|
| 173 |
|
| 174 |
with gr.Row():
|
| 175 |
json_output_farm = gr.Textbox(label="Farm Data JSON", visible=False)
|
|
@@ -179,18 +251,24 @@ with gr.Blocks() as app:
|
|
| 179 |
|
| 180 |
toggle_output = Toggle(label="JSON <-> YAML", value=False, info="Toggle Output Data", interactive=True, visible=False)
|
| 181 |
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])
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
inputs=survey2_submission_ID,
|
| 186 |
-
outputs=[json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials, toggle_output]
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
data_submit_button.click(
|
| 190 |
-
lambda button_type: process_survey("data", button_type),
|
| 191 |
inputs=survey_submission_ID,
|
| 192 |
-
outputs=[get_survey_back,
|
| 193 |
)
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
# This starts the program
|
| 196 |
app.launch()
|
|
|
|
| 11 |
global parsed_input_data
|
| 12 |
global original_outputs, yaml_outputs
|
| 13 |
|
| 14 |
+
#def process_survey(button_type, survey_submission_ID):
|
| 15 |
+
# """
|
| 16 |
+
# Gets the data from the survey submission, then processes it with models to get output (data-filled) JSON Schemas.
|
| 17 |
+
# parse_survey_stack_data and parse_survey_stack_parameters complete the "back-end" data processing and can be found in a separate file
|
| 18 |
+
#
|
| 19 |
+
# Args:
|
| 20 |
+
# 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.
|
| 21 |
+
# survey_submission_ID: (str) Survey Submission ID (for retrieving survey)
|
| 22 |
+
#
|
| 23 |
+
# Returns:
|
| 24 |
+
# (1) All front-end interactivity for hiding already used elements and showing new ones
|
| 25 |
+
# (2) when button-type == 'data', the parsed input data
|
| 26 |
+
# (3) when button-type == 'param', 3 JSONs representing the schemas for farm activity, interactions, and scientific trials
|
| 27 |
+
# """
|
| 28 |
+
# if button_type == 'data':
|
| 29 |
+
# survey_id = "671fc52152e3225d7de349e6"
|
| 30 |
+
#
|
| 31 |
+
# else:
|
| 32 |
+
# survey_id = "671fe2d352e3225d7de34afc"
|
| 33 |
+
#
|
| 34 |
+
# base_url = "https://app.surveystack.io/api/submissions"
|
| 35 |
+
# params = {
|
| 36 |
+
# "survey": survey_id,
|
| 37 |
+
# }
|
| 38 |
+
#
|
| 39 |
+
# encoded_params = urlencode(params)
|
| 40 |
+
#
|
| 41 |
+
# match_param = f'{{"_id": {{"$oid": "{survey_submission_ID}"}}}}'
|
| 42 |
+
#
|
| 43 |
+
# survey_url = f"{base_url}?{encoded_params}&match={match_param}"
|
| 44 |
+
#
|
| 45 |
+
# print("Button Type: " + button_type)
|
| 46 |
+
# print("PROCESS_SURVEY")
|
| 47 |
+
# print(survey_url)
|
| 48 |
+
#
|
| 49 |
+
# try:
|
| 50 |
+
# global original_outputs, yaml_outputs
|
| 51 |
+
# response = requests.get(survey_url)
|
| 52 |
+
# response.raise_for_status()
|
| 53 |
+
# data = response.json()
|
| 54 |
+
#
|
| 55 |
+
# print("DATA FROM SURVEY")
|
| 56 |
+
# print(data)
|
| 57 |
+
#
|
| 58 |
+
# if button_type == 'data':
|
| 59 |
+
# global parsed_input_data
|
| 60 |
+
# parsed_input_data = parse_survey_stack_data(data)
|
| 61 |
+
# return parsed_input_data, gr.HTML(visible=True), gr.Textbox(visible=False), gr.Button(visible=False), gr.Textbox(visible=True), gr.Button(visible=True)
|
| 62 |
+
# else:
|
| 63 |
+
# parsed_param_data = parse_survey_stack_parameters(data)
|
| 64 |
+
# parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_input_data, parsed_param_data)
|
| 65 |
+
# original_outputs = [parsed_farm_json, parsed_interactions_json, parsed_trial_json]
|
| 66 |
+
# yaml_outputs = []
|
| 67 |
+
# 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)
|
| 68 |
+
#
|
| 69 |
+
# except Exception as e:
|
| 70 |
+
# return f"An error occurred while fetching the survey data: {e}"
|
| 71 |
+
|
| 72 |
+
def process_new_survey(survey_submission_ID):
|
| 73 |
"""
|
| 74 |
Gets the data from the survey submission, then processes it with models to get output (data-filled) JSON Schemas.
|
| 75 |
parse_survey_stack_data and parse_survey_stack_parameters complete the "back-end" data processing and can be found in a separate file
|
|
|
|
| 80 |
|
| 81 |
Returns:
|
| 82 |
(1) All front-end interactivity for hiding already used elements and showing new ones
|
| 83 |
+
(2) the parsed input data
|
| 84 |
+
(3) 3 JSONs representing the schemas for farm activity, interactions, and scientific trials
|
| 85 |
"""
|
| 86 |
+
survey_id = "671fc52152e3225d7de349e6"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
base_url = "https://app.surveystack.io/api/submissions"
|
| 89 |
params = {
|
|
|
|
| 96 |
|
| 97 |
survey_url = f"{base_url}?{encoded_params}&match={match_param}"
|
| 98 |
|
| 99 |
+
print("PROCESSING_SURVEY")
|
|
|
|
| 100 |
print(survey_url)
|
| 101 |
|
| 102 |
try:
|
| 103 |
+
global original_outputs, yaml_outputs, parsed_input_data
|
| 104 |
response = requests.get(survey_url)
|
| 105 |
response.raise_for_status()
|
| 106 |
data = response.json()
|
|
|
|
| 108 |
print("DATA FROM SURVEY")
|
| 109 |
print(data)
|
| 110 |
|
| 111 |
+
parsed_input_data = parse_survey_stack_data(data)
|
| 112 |
+
parsed_param_data = parse_survey_stack_parameters(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
parsed_farm_json, parsed_interactions_json, parsed_trial_json = process_specifications(parsed_input_data, parsed_param_data)
|
| 115 |
+
original_outputs = [parsed_farm_json, parsed_interactions_json, parsed_trial_json]
|
| 116 |
+
yaml_outputs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
return parsed_input_data, parsed_farm_json, parsed_interactions_json, parsed_trial_json, gr.Textbox(visible=True), gr.Textbox(visible=True), gr.Textbox(visible=True), Toggle(visible=True)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"An error occurred while fetching the survey data: {e}"
|
| 121 |
+
|
| 122 |
+
#def display_parameter_survey():
|
| 123 |
+
# """
|
| 124 |
+
# Display parameter survey based on the IDs necessary to render the survey
|
| 125 |
+
#
|
| 126 |
+
# Args:
|
| 127 |
+
# None
|
| 128 |
+
#
|
| 129 |
+
# Returns:
|
| 130 |
+
# HTML of survey in an iframe which gradio renders automagically in the variable parameter_survey_output
|
| 131 |
+
# """
|
| 132 |
+
# base_url = "https://app.surveystack.io/groups/"
|
| 133 |
+
# group_id = "5e95e368fbbf75000146a006" # Group ID "Our-Sci"
|
| 134 |
+
# survey_id = "671fe2d352e3225d7de34afc" # survey id for parameter survey
|
| 135 |
+
#
|
| 136 |
+
# # Construct the full URL using urljoin to ensure proper formatting
|
| 137 |
+
# survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
|
| 138 |
+
#
|
| 139 |
+
# print("PARAMETER_SURVEY")
|
| 140 |
+
# print(survey_url)
|
| 141 |
+
#
|
| 142 |
+
#
|
| 143 |
+
# iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
| 144 |
+
#
|
| 145 |
+
# return iframe_html
|
| 146 |
+
#
|
| 147 |
+
#def display_data_survey():
|
| 148 |
+
# """
|
| 149 |
+
# Display data survey based on the IDs necessary to render the survey
|
| 150 |
+
#
|
| 151 |
+
# Args:
|
| 152 |
+
# None
|
| 153 |
+
#
|
| 154 |
+
# Returns:
|
| 155 |
+
# HTML of survey in an iframe which gradio renders automagically in the variable data_survey_output
|
| 156 |
+
# """
|
| 157 |
+
# base_url = "https://app.surveystack.io/groups/"
|
| 158 |
+
# group_id = "5e95e368fbbf75000146a006" # Group ID "Our-Sci"
|
| 159 |
+
# survey_id = "671fc52152e3225d7de349e6" # survey id for data survey
|
| 160 |
+
#
|
| 161 |
+
# # Construct the full URL using urljoin to ensure proper formatting
|
| 162 |
+
# survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
|
| 163 |
+
#
|
| 164 |
+
# print("DATA_SURVEY")
|
| 165 |
+
# print(survey_url)
|
| 166 |
+
#
|
| 167 |
+
# iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
| 168 |
+
# return iframe_html
|
| 169 |
|
| 170 |
+
def display_new_survey():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
"""
|
| 172 |
Display data survey based on the IDs necessary to render the survey
|
| 173 |
|
|
|
|
| 179 |
"""
|
| 180 |
base_url = "https://app.surveystack.io/groups/"
|
| 181 |
group_id = "5e95e368fbbf75000146a006" # Group ID "Our-Sci"
|
| 182 |
+
survey_id = "673b4994aef86f0533b3546c" # survey id for data survey
|
| 183 |
|
| 184 |
# Construct the full URL using urljoin to ensure proper formatting
|
| 185 |
survey_url = urljoin(base_url, f"{group_id}/surveys/{survey_id}/submissions/new")
|
| 186 |
|
| 187 |
+
print("SURVEY SURVEY")
|
| 188 |
print(survey_url)
|
| 189 |
|
| 190 |
iframe_html = f'<iframe src="{survey_url}" width="100%" height="600px"></iframe>'
|
|
|
|
| 228 |
with gr.Blocks() as app:
|
| 229 |
# This is the main driver of the program that sets up the gradio front end pieces, as well as specific interactivity with buttons
|
| 230 |
with gr.Row():
|
| 231 |
+
survey_output = gr.HTML(value=display_new_survey())
|
| 232 |
|
| 233 |
survey_submission_ID = gr.Textbox(label="Before submitting your surveystack survey, PLEASE record your submission ID. Type it here.")
|
| 234 |
|
| 235 |
+
#data_submit_button = gr.Button("Parse Data")
|
| 236 |
|
| 237 |
get_survey_back = gr.Textbox(label="Data from SurveyStack")
|
| 238 |
|
| 239 |
+
#with gr.Row():
|
| 240 |
+
# parameter_survey_output = gr.HTML(value=display_parameter_survey(), visible=False)
|
| 241 |
|
| 242 |
+
#survey2_submission_ID = gr.Textbox(label="Before submitting your surveystack survey, PLEASE record your submission ID. Type it here.", visible=False)
|
| 243 |
|
| 244 |
+
submit_button = gr.Button("Create JSONs", visible=False)
|
| 245 |
|
| 246 |
with gr.Row():
|
| 247 |
json_output_farm = gr.Textbox(label="Farm Data JSON", visible=False)
|
|
|
|
| 251 |
|
| 252 |
toggle_output = Toggle(label="JSON <-> YAML", value=False, info="Toggle Output Data", interactive=True, visible=False)
|
| 253 |
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])
|
| 254 |
+
|
| 255 |
+
submit_button.click(
|
| 256 |
+
fn=process_survey(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
inputs=survey_submission_ID,
|
| 258 |
+
outputs=[get_survey_back, json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials, toggle_output]
|
| 259 |
)
|
| 260 |
|
| 261 |
+
#param_submit_button.click(
|
| 262 |
+
# lambda button_type: process_survey("param", button_type),
|
| 263 |
+
# inputs=survey2_submission_ID,
|
| 264 |
+
# outputs=[json_output_farm, json_output_interactions, json_output_trials, json_output_farm, json_output_interactions, json_output_trials, toggle_output]
|
| 265 |
+
#)
|
| 266 |
+
|
| 267 |
+
#data_submit_button.click(
|
| 268 |
+
# lambda button_type: process_survey("data", button_type),
|
| 269 |
+
# inputs=survey_submission_ID,
|
| 270 |
+
# outputs=[get_survey_back, parameter_survey_output, data_survey_output, data_submit_button, survey2_submission_ID, param_submit_button]
|
| 271 |
+
#)
|
| 272 |
+
|
| 273 |
# This starts the program
|
| 274 |
app.launch()
|