Spaces:
Sleeping
Sleeping
#!/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) # Avoid changing the working directory | |
# Update JSON with directory settings | |
with open('stored_variables.json', 'r') as file: | |
data = json.load(file) | |
data.update({'base_dir': base_dir, 'set_path': set_path}) | |
with open('stored_variables.json', 'w') as file: | |
json.dump(data, file, indent=4) | |
# 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 | |
# Assuming csv_files should be a DataFrame constructed from ls_samples | |
csv_files = pd.DataFrame({'CSV Files': ls_samples}) | |
md_files = [file for file in os.listdir(metadata_dir) if file.endswith(".tif.csv")] | |
print(md_files) | |
metadata_files_df = pd.DataFrame({'Selected_Metadata_files' : [file for file in os.listdir(metadata_dir) if file.endswith(".tif.csv")]}) | |
print(metadata_files_df) | |
# 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): | |
with open('stored_variables.json', 'r') as file: | |
data = json.load(file) | |
data['ls_samples'] = ls_samples | |
with open('stored_variables.json', 'w') as file: | |
json.dump(data, file, indent=4) | |
print("ls_samples updated in JSON 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', 'r') as file: | |
data = json.load(file) | |
data['selected_metadata_files'] = selected_metadata_files | |
with open('stored_variables.json', 'w') as file: | |
json.dump(data, file, indent=4) | |
print("selected_metadata_files updated in JSON file.") | |
# Define the widgets | |
file_input = pn.widgets.TextInput( | |
name="File Path", | |
placeholder="Enter the path to your directory" | |
) | |
set_name = pn.widgets.TextInput( | |
name="Set Name", | |
placeholder="Enter the Set" | |
) | |
store_button = pn.widgets.Button(name='Store Values') | |
store_button.on_click(store_values) | |
checkbox_group = pn.widgets.CheckBoxGroup(name='Select CSV Files', options=[], value=[]) | |
checkbox_group.param.watch(update_ls_samples, 'value') | |
metadata_checkbox_group = pn.widgets.CheckBoxGroup(name='Select Metadata Files', options=[], value=[]) | |
metadata_checkbox_group.param.watch(update_metadata_files, 'value') | |
save_button = pn.widgets.Button(name='Save Files') | |
save_button.on_click(save_selected_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() |