File size: 4,021 Bytes
fb83c5b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import gradio as gr
import os
from .common_gui import list_files, scriptdir, create_refresh_button
from .custom_logging import setup_logging
# Set up logging
log = setup_logging()
class ConfigurationFile:
"""
A class to handle configuration file operations in the GUI.
"""
def __init__(
self, headless: bool = False, config_dir: str = None, config: dict = {}
):
"""
Initialize the ConfigurationFile class.
Parameters:
- headless (bool): Whether to run in headless mode.
- config_dir (str): The directory for configuration files.
"""
self.headless = headless
self.config = config
# Sets the directory for storing configuration files, defaults to a 'presets' folder within the script directory.
self.current_config_dir = self.config.get(
"config_dir", os.path.join(scriptdir, "presets")
)
# Initialize the GUI components for configuration.
self.create_config_gui()
def list_config_dir(self, path: str) -> list:
"""
List directories in the data directory.
Parameters:
- path (str): The path to list directories from.
Returns:
- list: A list of directories.
"""
self.current_config_dir = path if not path == "" else "."
# Lists all .json files in the current configuration directory, used for populating dropdown choices.
return list(list_files(self.current_config_dir, exts=[".json"], all=True))
def create_config_gui(self) -> None:
"""
Create the GUI for configuration file operations.
"""
# Starts a new group in the GUI for better layout organization.
with gr.Group():
# Creates a row within the group to align elements horizontally.
with gr.Row():
# Dropdown for selecting or entering the name of a configuration file.
self.config_file_name = gr.Dropdown(
label="Load/Save Config file",
choices=[self.config.get("config_dir", "")] + self.list_config_dir(self.current_config_dir),
value=self.config.get("config_dir", ""),
interactive=True,
allow_custom_value=True,
)
# Button to refresh the list of configuration files in the dropdown.
create_refresh_button(
self.config_file_name,
lambda: None, # Placeholder for potential future functionality.
lambda: {
"choices": [""] + self.list_config_dir(self.current_config_dir)
},
"open_folder_small",
)
# Buttons for opening, saving, and loading configuration files, displayed conditionally based on headless mode.
self.button_open_config = gr.Button(
"📂",
elem_id="open_folder_small",
elem_classes=["tool"],
visible=(not self.headless),
)
self.button_save_config = gr.Button(
"💾",
elem_id="open_folder_small",
elem_classes=["tool"],
)
self.button_load_config = gr.Button(
"↩️ ",
elem_id="open_folder_small",
elem_classes=["tool"],
)
# Handler for change events on the configuration file dropdown, allowing dynamic update of choices.
self.config_file_name.change(
fn=lambda path: gr.Dropdown(choices=[""] + self.list_config_dir(path)),
inputs=self.config_file_name,
outputs=self.config_file_name,
show_progress=False,
)
|