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:
- Get the full path to the uploaded file, or
- 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)
FilePickerFilereturned byFile Picker.pick files(), you can query the path withf.pathand use it to open it with Pandas or Polars.FilePickerUploadFileAPI 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.pathinsidehandle_pick_filesas mentioned in the comments. Not sure if it's clear or not butfilein your code is a list. pick_files still returns a list with allow_multiple=False (the default).