flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
10.27k stars 397 forks source link

get_upload_url does not work with Pyodide static website #2984

Open DateOrMage opened 3 months ago

DateOrMage commented 3 months ago

I'm making a web app with the ability to upload data. There was a problem using the get_upload_url method. When running from source code everything works correctly. But after building the web application (I use the command flet build web --no-web-splash) and deploying it on localhost, the file upload option does not work. The following error appears in the browser console (Google Chrome): <PyodideTask finished name='Task-26' coro=<app_async..on_event() done, defined at C:\Users\Aleksei\AppData\Local\Temp\ serious_python_temp12899567__pypackages__\flet\flet.py:52> exception=Exception('getUploadUrl command is not supported.')>

Part of code (with upload files) to reproduce the issue:

def get_df_list(data_names: str) -> list[pd.DataFrame]:
    data_names_list = data_names.split('\n')
    for idx_name in range(len(data_names_list)):
        data_names_list[idx_name] = os.path.join('uploads', data_names_list[idx_name])
    df_list = []
    for path_name in data_names_list:
        df = pd.read_csv(path_name)
        df_list.append(df)
    del df
 # --- loading files ---

    def pick_files_result(e):
        selected_files.value = None
        selected_files.update()
        upload_list = []
        if pick_files_dialog.result is not None and pick_files_dialog.result.files is not None:
            for f in pick_files_dialog.result.files:
                upload_list.append(
                    ft.FilePickerUploadFile(
                        f.name,
                        upload_url=page.get_upload_url(f.name, 600),
                    )
                )
            pr.visible = True
            pr.update()
            time.sleep(3)  # TODO del this
            pick_files_dialog.upload(upload_list)

        print(upload_list)
        if len(upload_list) > 0:
            for i in range(len(upload_list)):
                if i == 0:
                    selected_files.value = upload_list[i].name
                else:
                    selected_files.value += f'\n{upload_list[i].name}'
        else:
            selected_files.value = "Файл не выбран!"

        if selected_files.value != "Файл не выбран!":
            get_df_list(selected_files.value)
        pr.visible = False
        pr.update()
        selected_files.update()
        files_names_list.value = str(os.listdir('uploads'))
        test_path_dir_uploads.update()
        print(selected_files)

    pick_files_dialog = ft.FilePicker(on_result=pick_files_result)
    selected_files = ft.Text(color=ft.colors.BLACK, text_align=ft.MainAxisAlignment.CENTER)

    page.overlay.append(pick_files_dialog)

    pr = ft.Row(
        [
            ft.ProgressRing(width=16, height=16, stroke_width=2, color=ft.colors.BLACK),
            ft.Text(color=ft.colors.BLACK, text_align=ft.MainAxisAlignment.CENTER, value="Загрузка файлов...")
        ],
        visible=False,
        alignment=ft.MainAxisAlignment.CENTER,
    )

    test_path = ft.Row(
        [
            ft.Text(color=ft.colors.BLACK, text_align=ft.MainAxisAlignment.CENTER, value=os.getcwd())
        ],
        visible=True,
        alignment=ft.MainAxisAlignment.CENTER,
    )

    test_path_dir = ft.Row(
        [
            ft.Text(color=ft.colors.BLACK, text_align=ft.MainAxisAlignment.CENTER, value=str(os.listdir()))
        ],
        visible=True,
        alignment=ft.MainAxisAlignment.CENTER,
    )

    files_names_list = ft.Text(color=ft.colors.BLACK, text_align=ft.MainAxisAlignment.CENTER, value=str(os.listdir('uploads')))

    test_path_dir_uploads = ft.Row(
        [
            files_names_list,
        ],
        visible=True,
        alignment=ft.MainAxisAlignment.CENTER,
    )

    upload_obj = ft.Column(
        [
          ft.Container(
              content=ft.Column(
                  [
                      ft.ElevatedButton(
                          'Выберите файл',
                          icon=ft.icons.UPLOAD_FILE,
                          on_click=lambda _: pick_files_dialog.pick_files(
                              allow_multiple=True,
                              allowed_extensions=['csv'],
                          ),
                      ),
                      selected_files,
                      pr,
                      test_path,  # TODO del this
                      test_path_dir,  # TODO del this
                      test_path_dir_uploads,  # TODO del this
                  ],
                  horizontal_alignment=ft.CrossAxisAlignment.CENTER,
              ),
              height=page.window_height - my_app_bar_h,
              alignment=ft.alignment.center,
              padding=ft.padding.only(top=my_app_bar_h),
          ),
        ],
        visible=False,
    )

Describe the results you received:

Flet version (pip show flet):

Name: flet
Version: 0.21.2
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0
Location: c:\users\aleksei\desktop\drillingmixturewebapp\venv\lib\site-packages
Requires: cookiecutter, fastapi, flet-runtime, packaging, qrcode, uvicorn, watchdog
Required-by:

Give your requirements.txt file (don't pip freeze, instead give direct packages):

flet==0.21.2
pandas==1.5.3
numpy==1.26.1

Operating system: Windows 10:

Additional environment details: Python 3.10

C69Xg3sg9PAvYK commented 3 months ago

I don't think there is support for pandas yet, try using inbuilt import csv