Skip to content

FilePicker

A control that allows you to use the native file explorer to pick single or multiple files, with extensions filtering support and upload.

Important

In Linux, the FilePicker control depends on Zenity when running Flet as an app. This is not a requirement when running Flet in a browser.

To install Zenity on Ubuntu/Debian run the following commands:

sudo apt-get install zenity

Inherits: Service

Events

Methods

  • get_directory_path

    Selects a directory and returns its absolute path.

  • pick_files

    Retrieves the file(s) from the underlying platform.

  • save_file

    Opens a save file dialog which lets the user select a file path and a file name

  • upload

    Uploads selected files to specified upload URLs.

Examples#

Live example

Pick, save, and get directory paths#

import flet as ft


def main(page: ft.Page):
    async def handle_pick_files(e: ft.Event[ft.Button]):
        files = await ft.FilePicker().pick_files(allow_multiple=True)
        selected_files.value = (
            ", ".join(map(lambda f: f.name, files)) if files else "Cancelled!"
        )

    async def handle_save_file(e: ft.Event[ft.Button]):
        save_file_path.value = await ft.FilePicker().save_file()

    async def handle_get_directory_path(e: ft.Event[ft.Button]):
        directory_path.value = await ft.FilePicker().get_directory_path()

    page.add(
        ft.Row(
            controls=[
                ft.Button(
                    content="Pick files",
                    icon=ft.Icons.UPLOAD_FILE,
                    on_click=handle_pick_files,
                ),
                selected_files := ft.Text(),
            ]
        ),
        ft.Row(
            controls=[
                ft.Button(
                    content="Save file",
                    icon=ft.Icons.SAVE,
                    on_click=handle_save_file,
                    disabled=page.web,  # disable this button in web mode
                ),
                save_file_path := ft.Text(),
            ]
        ),
        ft.Row(
            controls=[
                ft.Button(
                    content="Open directory",
                    icon=ft.Icons.FOLDER_OPEN,
                    on_click=handle_get_directory_path,
                    disabled=page.web,  # disable this button in web mode
                ),
                directory_path := ft.Text(),
            ]
        ),
    )


ft.run(main)

pick-save-and-get-directory-path

Pick and upload files#

#
# Example of picking and uploading files with progress indication
#
# Run this example with:
#    export FLET_SECRET_KEY=<some_secret_key>
#    uv run flet run --web examples/controls/file_picker/pick_and_upload.py
#
from dataclasses import dataclass, field

import flet as ft


@dataclass
class State:
    file_picker: ft.FilePicker | None = None
    picked_files: list[ft.FilePickerFile] = field(default_factory=list)


state = State()


def main(page: ft.Page):
    prog_bars: dict[str, ft.ProgressRing] = {}

    def on_upload_progress(e: ft.FilePickerUploadEvent):
        prog_bars[e.file_name].value = e.progress

    async def handle_files_pick(e: ft.Event[ft.Button]):
        state.file_picker = ft.FilePicker(on_upload=on_upload_progress)
        files = await state.file_picker.pick_files(allow_multiple=True)
        print("Picked files:", files)
        state.picked_files = files

        # update progress bars
        upload_button.disabled = len(files) == 0
        prog_bars.clear()
        upload_progress.controls.clear()
        for f in files:
            prog = ft.ProgressRing(value=0, bgcolor="#eeeeee", width=20, height=20)
            prog_bars[f.name] = prog
            upload_progress.controls.append(ft.Row([prog, ft.Text(f.name)]))

    async def handle_file_upload(e: ft.Event[ft.Button]):
        upload_button.disabled = True
        await state.file_picker.upload(
            files=[
                ft.FilePickerUploadFile(
                    name=file.name,
                    upload_url=page.get_upload_url(f"dir/{file.name}", 60),
                )
                for file in state.picked_files
            ]
        )

    page.add(
        ft.Button(
            content="Select files...",
            icon=ft.Icons.FOLDER_OPEN,
            on_click=handle_files_pick,
        ),
        upload_progress := ft.Column(),
        upload_button := ft.Button(
            content="Upload",
            icon=ft.Icons.UPLOAD,
            on_click=handle_file_upload,
            disabled=True,
        ),
    )


ft.run(main, upload_dir="examples")

pick-and-upload

Events#

on_upload class-attribute instance-attribute #

on_upload: EventHandler[FilePickerUploadEvent] | None = None

Called when a file upload progress is updated.

Methods#

get_directory_path async #

get_directory_path(
    dialog_title: str | None = None,
    initial_directory: str | None = None,
) -> str | None

Selects a directory and returns its absolute path.

Parameters:

  • dialog_title (str | None, default: None ) –

    The title of the dialog window. Defaults to [`FilePicker.

  • initial_directory (str | None, default: None ) –

    The initial directory where the dialog should open.

Returns:

  • str | None

    The selected directory path or None if the dialog was cancelled.

Raises:

pick_files async #

pick_files(
    dialog_title: str | None = None,
    initial_directory: str | None = None,
    file_type: FilePickerFileType = ANY,
    allowed_extensions: list[str] | None = None,
    allow_multiple: bool = False,
) -> list[FilePickerFile]

Retrieves the file(s) from the underlying platform.

Parameters:

  • dialog_title (str | None, default: None ) –

    The title of the dialog window.

  • initial_directory (str | None, default: None ) –

    The initial directory where the dialog should open.

  • file_type (FilePickerFileType, default: ANY ) –

    The file types allowed to be selected.

  • allow_multiple (bool, default: False ) –

    Allow the selection of multiple files at once.

  • allowed_extensions (list[str] | None, default: None ) –

    The allowed file extensions. Has effect only if file_type is FilePickerFileType.CUSTOM.

Returns:

save_file async #

save_file(
    dialog_title: str | None = None,
    file_name: str | None = None,
    initial_directory: str | None = None,
    file_type: FilePickerFileType = ANY,
    allowed_extensions: list[str] | None = None,
    src_bytes: bytes | None = None,
) -> str | None

Opens a save file dialog which lets the user select a file path and a file name to save a file.

Note
  • On desktop this method only opens a dialog for the user to select a location and file name, and returns the chosen path. The file itself is not created or saved.

Parameters:

  • dialog_title (str | None, default: None ) –

    The title of the dialog window.

  • file_name (str | None, default: None ) –

    The default file name.

  • initial_directory (str | None, default: None ) –

    The initial directory where the dialog should open.

  • file_type (FilePickerFileType, default: ANY ) –

    The file types allowed to be selected.

  • src_bytes (bytes | None, default: None ) –

    The contents of a file. Must be provided in web, iOS or Android modes.

  • allowed_extensions (list[str] | None, default: None ) –

    The allowed file extensions. Has effect only if file_type is FilePickerFileType.CUSTOM.

Raises:

  • ValueError

    If src_bytes is not provided, when called in web mode, on iOS or Android.

  • ValueError

    If file_name is not provided in web mode.

upload async #

upload(files: list[FilePickerUploadFile])

Uploads selected files to specified upload URLs.

Before calling this method, pick_files() must be called, so that the internal file picker selection is not empty.

Parameters: