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'' # # 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'' # 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'' 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()