| 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, process_specifications |
|
|
| from script_for_automation import drive_process |
|
|
| global original_outputs, yaml_outputs |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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_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_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" |
| survey_id = "673b4994aef86f0533b3546c" |
|
|
| |
| 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: |
| |
| 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.") |
| |
| |
|
|
| get_survey_back = gr.Textbox(label="Data from SurveyStack") |
|
|
| |
| |
| |
| |
|
|
| 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")] |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| app.launch() |