0

I am using Flet 0.80.4 with Python 3.11.

The user should be able to upload a file via a button. I want to process this file with Polars or Pandas. I found some examples of how to handle file uploads, but they rely on the on_result method, which has been deprecated in the current Flet version.

I am looking for a way to either:

  1. Get the full path to the uploaded file, or
  2. Use an alternative approach to work with the uploaded file directly in memory (without saving it to a temporary directory).

My func:

def main(page: ft.Page):
    page.title = 'Title'
    page.window.width = 450
    page.window.height = 350
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    async def handle_pick_files(e: ft.Event[ft.Button]):
        file = await ft.FilePicker().pick_files(
            file_type=ft.FilePickerFileType.CUSTOM,
            allowed_extensions=['xlsx'],
        )
        selected_files.value = (
            ", ".join(map(lambda f: f.name, file))
        )

    load_file_button = ft.Button(
        content='UPLOAD',
        icon=ft.Icons.UPLOAD_FILE,
        on_click=handle_pick_files,
    )

    page.add(
        ft.Row(
            controls=[
                load_file_button,
                selected_files := ft.Text(),
            ],
            alignment=ft.MainAxisAlignment.CENTER
        ),
    )


ft.run(main)
2
  • 1
    I think for each object of type FilePickerFile returned by File Picker.pick files(), you can query the path with f.path and use it to open it with Pandas or Polars. Commented Jan 25 at 22:03
  • There is a FilePickerUploadFile API docs.flet.dev/services/filepicker/#pick-and-upload-files but it seems it's only for flet-web (get_upload_url() raises for me without --web). For "desktop", it seems it's up to you to process the .path inside handle_pick_files as mentioned in the comments. Not sure if it's clear or not but file in your code is a list. pick_files still returns a list with allow_multiple=False (the default). Commented Jan 26 at 9:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.