#!/usr/bin/env python # coding: utf-8 import panel as pn import os import pandas as pd import json pn.extension() # Global variables to store the input values input_path = "" set_path = "" ls_samples = [] metadata_files = [] selected_metadata_files = [] # Define a callback to store values when the button is clicked def store_values(event): global input_path, set_path, ls_samples, metadata_files input_path = file_input.value set_path = set_name.value # Update the global variables global base_dir, input_data_dir, output_data_dir, output_images_dir, metadata_dir, metadata_images_dir present_dir = os.path.dirname(os.path.realpath(__file__)) input_path = os.path.join(present_dir, input_path) base_dir = input_path project_name = set_path step_suffix = 'qc_eda' previous_step_suffix_long = "" input_data_dir = os.path.join(base_dir, project_name + "_data") output_data_dir = os.path.join(base_dir, project_name + "_" + step_suffix) output_images_dir = os.path.join(output_data_dir, "images") metadata_dir = os.path.join(base_dir, project_name + "_metadata") metadata_images_dir = os.path.join(metadata_dir, "images") # Create directories if they don't already exist for d in [base_dir, input_data_dir, output_data_dir, output_images_dir, metadata_dir, metadata_images_dir]: if not os.path.exists(d): print(f"Creation of the {d} directory...") os.makedirs(d) else: print(f"The {d} directory already exists!") os.chdir(input_data_dir) # Create a DataFrame with the directory paths data = { 'Directory': ['Base Directory', 'Input Data Directory', 'Output Data Directory', 'Output Images Directory', 'Metadata Directory', 'Metadata Images Directory'], 'Path': [base_dir, input_data_dir, output_data_dir, output_images_dir, metadata_dir, metadata_images_dir] } df = pd.DataFrame(data) # List CSV files in the input_data_dir ls_samples = [sample for sample in os.listdir(input_data_dir) if sample.endswith(".csv")] print(f"The following CSV files were detected:\n\n{ls_samples}\n\nin {input_data_dir} directory.") # List CSV files in the metadata_dir metadata_files = [file for file in os.listdir(metadata_dir) if file.endswith(".tif.csv")] print(f"The following metadata CSV files were detected:\n\n{metadata_files}\n\nin {metadata_dir} directory.") # Update the CheckBoxGroup options checkbox_group.options = ls_samples metadata_checkbox_group.options = metadata_files # Create a DataFrame with the CSV files csv_files = pd.DataFrame({'CSV Files': ls_samples}) metadata_files_df = pd.DataFrame({'Metadata Files': metadata_files}) # Update the output panes output_pane_1.object = f"**File Path:** {input_path}\n\n**Set Name:** {set_path}" output_pane_2.object = df.to_html(index=False) output_pane_3.object = csv_files.to_html(index=False) output_pane_4.object = metadata_files_df.to_html(index=False) # Define a callback to save the selected CSV files when the button is clicked def save_selected_files(event): # Store the variables in a JSON file with open('stored_variables.json', 'w') as file: json.dump({'base_dir': base_dir, 'set_path': set_path}, file) # Define a callback to update the ls_samples list based on the selected options def update_ls_samples(event): global ls_samples ls_samples = event.new # Define a callback to update the metadata_files list based on the selected options def update_metadata_files(event): global metadata_files, selected_metadata_files metadata_files = event.new selected_metadata_files = event.new # Define a callback to save the selected metadata files when the button is clicked def save_selected_metadata_files(event): with open('stored_variables.json', 'w') as file: json.dump({'selected_metadata_files': selected_metadata_files}, file) # Define the widgets file_input = pn.widgets.TextInput( name="File Path", placeholder="Enter the path to your directory" ) # Define the set name set_name = pn.widgets.TextInput( name="Set Name", placeholder="Enter the Set" ) # Button to trigger storing values store_button = pn.widgets.Button(name='Store Values') store_button.on_click(store_values) # CheckBoxGroup to select CSV files checkbox_group = pn.widgets.CheckBoxGroup(name='Select CSV Files', options=[], value=[]) checkbox_group.param.watch(update_ls_samples, 'value') # CheckBoxGroup to select metadata files metadata_checkbox_group = pn.widgets.CheckBoxGroup(name='Select Metadata Files', options=[], value=[]) metadata_checkbox_group.param.watch(update_metadata_files, 'value') # Button to save selected CSV files save_button = pn.widgets.Button(name='Save Files') save_button.on_click(save_selected_files) # Button to save selected metadata files save_metadata_button = pn.widgets.Button(name='Save Metadata Files') save_metadata_button.on_click(save_selected_metadata_files) output_pane_1 = pn.pane.Markdown("") output_pane_2 = pn.pane.HTML("") output_pane_3 = pn.pane.HTML("") output_pane_4 = pn.pane.HTML("") # Create the cards card_1 = pn.Card( pn.Column( pn.pane.Markdown("### Input Form"), file_input, set_name, store_button, output_pane_1, sizing_mode="stretch_width" ), title="Input Form", sizing_mode="stretch_width" ) card_2 = pn.Card( pn.Column( pn.pane.Markdown("### Directory Paths"), output_pane_2, sizing_mode="stretch_width" ), title="Directory Paths", sizing_mode="stretch_width" ) card_3 = pn.Card( pn.Column( pn.pane.Markdown("### CSV Files"), output_pane_3, pn.pane.Markdown("### Selection of CSV files for the further analysis"), checkbox_group, save_button, sizing_mode="stretch_width" ), title="CSV Files", sizing_mode="stretch_width" ) card_4 = pn.Card( pn.Column( pn.pane.Markdown("### Metadata Files"), output_pane_4, pn.pane.Markdown("### Selection of Metadata Files"), metadata_checkbox_group, save_metadata_button, sizing_mode="stretch_width" ), title="Metadata Files", sizing_mode="stretch_width" ) # Create the main column with the cards main_column = pn.Column(card_1, card_2, card_3, card_4) # Create the GoldenTemplate with the main column app = pn.template.GoldenTemplate( site="Cyc-IF", title="Setup", main=[main_column], header_color="black", ) app.servable()