diff --git a/birdnet_analyzer/gui/multi_file.py b/birdnet_analyzer/gui/multi_file.py index 10ceab2a..ac426c3d 100644 --- a/birdnet_analyzer/gui/multi_file.py +++ b/birdnet_analyzer/gui/multi_file.py @@ -90,7 +90,8 @@ def build_multi_analysis_tab(): ) def select_directory_on_empty(): # Nishant - Function modified for For Folder selection - folder = gu.select_folder() + folder = gu.select_folder(state_key="batch-analysis-data-dir") + if folder: files_and_durations = gu.get_files_and_durations(folder) if len(files_and_durations) > 100: @@ -112,7 +113,7 @@ def select_directory_on_empty(): # Nishant - Function modified for For Folder s ) def select_directory_wrapper(): # Nishant - Function modified for For Folder selection - folder = gu.select_folder() + folder = gu.select_folder(state_key="batch-analysis-output-dir") return (folder, folder) if folder else ("", "") select_out_directory_btn.click( diff --git a/birdnet_analyzer/gui/review.py b/birdnet_analyzer/gui/review.py index e809e858..368b5249 100644 --- a/birdnet_analyzer/gui/review.py +++ b/birdnet_analyzer/gui/review.py @@ -222,12 +222,10 @@ def select_subdir(new_value: str, next_review_state: dict): return {review_state: next_review_state} def start_review(next_review_state): - initial_dir = loc.get_state("review-input-dir", "") - dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir) + dir_name = gu.select_folder(state_key="review-input-dir") if dir_name: - loc.set_state("review-input-dir", dir_name[0]) - next_review_state["input_directory"] = dir_name[0] + next_review_state["input_directory"] = dir_name specieslist = [e.name for e in os.scandir(next_review_state["input_directory"]) if e.is_dir()] if not specieslist: diff --git a/birdnet_analyzer/gui/species.py b/birdnet_analyzer/gui/species.py index 7fe70628..f1b2e3f9 100644 --- a/birdnet_analyzer/gui/species.py +++ b/birdnet_analyzer/gui/species.py @@ -34,14 +34,13 @@ def build_species_tab(): ) def select_directory_and_update_tb(name_tb): - initial_dir = loc.get_state("species-output-dir", "") - dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir) + dir_name = gu.select_folder(state_key="species-output-dir") if dir_name: - loc.set_state("species-output-dir", dir_name[0]) + loc.set_state("species-output-dir", dir_name) return ( - dir_name[0], - gr.Textbox(label=dir_name[0] + "\\", visible=True, value=name_tb), + dir_name, + gr.Textbox(label=dir_name[0] + os.sep, visible=True, value=name_tb), ) return None, name_tb diff --git a/birdnet_analyzer/gui/train.py b/birdnet_analyzer/gui/train.py index 35a14755..eca43859 100644 --- a/birdnet_analyzer/gui/train.py +++ b/birdnet_analyzer/gui/train.py @@ -19,15 +19,10 @@ def select_subdirectories(state_key=None): Returns: A tuples of (directory, list of subdirectories) or (None, None) if the dialog was canceled. """ - - initial_dir = loc.get_state(state_key, "") if state_key else "" - dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir) + dir_name = gu.select_folder(state_key=state_key) if dir_name: - if state_key: - loc.set_state(state_key, dir_name[0]) - - subdirs = utils.list_subdirectories(dir_name[0]) + subdirs = utils.list_subdirectories(dir_name) labels = [] for folder in subdirs: @@ -37,7 +32,7 @@ def select_subdirectories(state_key=None): if not label in labels: labels.append(label) - return dir_name[0], [[label] for label in sorted(labels)] + return dir_name, [[label] for label in sorted(labels)] return None, None @@ -224,14 +219,12 @@ def build_train_tab(): ) def select_directory_and_update_tb(): - initial_dir = loc.get_state("train-output-dir", "") - dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir) + dir_name = gu.select_folder(state_key="train-output-dir") if dir_name: - loc.set_state("train-output-dir", dir_name[0]) return ( - dir_name[0], - gr.Textbox(label=dir_name[0] + "\\", visible=True), + dir_name, + gr.Textbox(label=dir_name + os.sep, visible=True), gr.Radio(visible=True, interactive=True), ) @@ -404,11 +397,9 @@ def on_crop_select(new_crop_mode): ) def select_directory_and_update(): - initial_dir = loc.get_state("train-data-cache-file-output", "") - dir_name = gu._WINDOW.create_file_dialog(webview.FOLDER_DIALOG, directory=initial_dir) + dir_name = gu.select_folder(state_key="train-data-cache-file-output") if dir_name: - loc.set_state("train-data-cache-file-output", dir_name[0]) return ( dir_name[0], gr.Textbox(label=dir_name[0] + "\\", visible=True), diff --git a/birdnet_analyzer/gui/utils.py b/birdnet_analyzer/gui/utils.py index c38320d1..f3f4a682 100644 --- a/birdnet_analyzer/gui/utils.py +++ b/birdnet_analyzer/gui/utils.py @@ -28,7 +28,7 @@ APPDIR.mkdir(parents=True, exist_ok=True) - sys.stderr = sys.stdout = open(str(APPDIR / "logs.txt"), "w") + sys.stderr = sys.stdout = open(str(APPDIR / "logs.txt"), "a") cfg.ERROR_LOG_FILE = str(APPDIR / cfg.ERROR_LOG_FILE) FROZEN = True else: @@ -47,15 +47,19 @@ # Nishant - Following two functions (select_folder andget_files_and_durations) are written for Folder selection -def select_folder(): +def select_folder(state_key=None): from tkinter import Tk, filedialog tk = Tk() tk.withdraw() + initial_dir = loc.get_state(state_key, None) if state_key else None - folder_selected = filedialog.askdirectory() + folder_selected = filedialog.askdirectory(initialdir=initial_dir) tk.destroy() + if folder_selected and state_key: + loc.set_state(state_key, folder_selected) + return folder_selected diff --git a/birdnet_analyzer/localization.py b/birdnet_analyzer/localization.py index 3ccc203e..dd2d0c4a 100644 --- a/birdnet_analyzer/localization.py +++ b/birdnet_analyzer/localization.py @@ -28,7 +28,7 @@ def get_state_dict() -> dict: return json.load(f) except FileNotFoundError: try: - with open(STATE_SETTINGS_PATH, "w") as f: + with open(STATE_SETTINGS_PATH, "w", encoding="utf-8") as f: json.dump({}, f) return {} except Exception as e: