Get a directory path and store it with flet

Hi, as the title says, how can I use the filepicker flet widget and its get_directory_path to extract said directory path ? The documentation just says nothing, and as a beginner I just don’t understand how on earth I can do that. I just know that the option is here !

In this code I’d like the “Sélectionner un répertoire” button to call the filepicker. Then I would be able to go to a directory, select it, and store its path somewhere in the app, like in a variable or plain text or somewhere else. As a bonus, I’d like the selected path to appear below the button.

PS : I know it sounds like I’m just asking you to code for me, but trust me I’ve spent hours trying to do it by myself and I just don’t understand how it works !

Here is the code :

import flet as ft
from flet_route import Params, Basket
from flet import (Page, FilePicker, Text,
                  ElevatedButton, Row, Column, FilePickerResultEvent)
import subprocess

def parameters(page: ft.Page, params: Params, basket: Basket):

    return ft.View(
        "/parameters/",
        bgcolor='#FFFFFF',
        controls=[
            ft.Row(
                controls=[
                    ft.IconButton(
                        icon=ft.icons.ARROW_BACK,
                        icon_size=20,
                        on_click=lambda _: page.go("/")
                    )
                ],
                alignment=ft.MainAxisAlignment.START
            ),
            ft.Container(  # Conteneur qui centre la colonne
                #bgcolor=ft.Colors.YELLOW, UTILE POUR VOIR LA ZONE DU CONTENEUR
                content=ft.Column(
                    controls=[
                        ft.ElevatedButton(
                            "Sélectionner un répertoire",
                            on_click=lambda _: select_folder
                        ),
                    ],
                    alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER
                ),
                alignment=ft.alignment.center,  # Centre tout le contenu
                expand=True  # Fait en sorte que le container prenne tout l'espace
            )
        ],
        vertical_alignment=ft.MainAxisAlignment.CENTER  # Centre tout le contenu verticalement
    )

Thanks !

The call back lambda function needs to store the result somewhere outside of itself, preferably in an instance variable on a class. Something can then access the result and display it.

Thanks for the help. I managed to store it in a json file for the setting to be permanent, and as a global variable. I’m not really sure it’s a pretty code but it works !

Here is the working code in case someone else is as bad as me (I mean the code itself is useless by itself since it needs to be called by another python script of my project) :

import flet as ft
import json
import os
from flet_route import Params, Basket
from flet import FilePicker, FilePickerResultEvent
import subprocess

selected_folder_path = None
current_dir = os.path.dirname(os.path.abspath(__file__))
root_dir =  os.path.dirname(current_dir)
parameters_file = os.path.join(root_dir, 'parameters.json')

def parameters(page: ft.Page, params: Params, basket: Basket):
    global parameters_file
    global selected_folder_path
    
    music_folder = ft.Text()

    if os.path.isfile(parameters_file) and os.access(parameters_file, os.R_OK):
        with open(parameters_file) as f:
            data = json.load(f)
            selected_folder_path = data.get('selected_folder', None)
            if selected_folder_path:
                music_folder.value = f"Dossier sélectionné : {selected_folder_path}"
            else:
                music_folder.value = "Aucun dossier sélectionné..."

    file_picker = FilePicker(
        on_result=lambda e: handle_folder_selected(e, music_folder)
    )
    page.overlay.append(file_picker)

    def handle_folder_selected(e: FilePickerResultEvent, text_element: ft.Text):
        global selected_folder_path
        if e.path:
            selected_folder_path = e.path
            text_element.value = f"Dossier sélectionné : {selected_folder_path}"
            with open(parameters_file, 'w') as f:
                json.dump({'selected_folder': selected_folder_path}, f)
            page.update()

    #basket['music_folder'] = music_folder

    return ft.View(
        "/parameters/",
        bgcolor='#FFFFFF',
        controls=[
            ft.Row(
                controls=[
                    ft.IconButton(
                        icon=ft.icons.ARROW_BACK,
                        icon_size=20,
                        on_click=lambda _: page.go("/")
                    )
                ],
                alignment=ft.MainAxisAlignment.START
            ),
            ft.Container(  # Conteneur qui centre la colonne
                #bgcolor=ft.Colors.YELLOW, UTILE POUR VOIR LA ZONE DU CONTENEUR
                content=ft.Column(
                    controls=[
                        ft.ElevatedButton(
                            "Sélectionner un répertoire",
                            on_click=lambda _: file_picker.get_directory_path()
                        ),
                        music_folder
                    ],
                    alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER
                ),
                alignment=ft.alignment.center,  # Centre tout le contenu
                expand=True  # Fait en sorte que le container prenne tout l'espace
            )
        ],
        vertical_alignment=ft.MainAxisAlignment.CENTER  # Centre tout le contenu verticalement
    )