Open AngRotanak opened 10 months ago
I started playing with Flet yesterday and had a similar issue. The code below is rough but uploads are working. For some reason, the last increment in the progress bar is very slow for large files. I'm also guessing you could permanently add 'FLET_SECRET_KEY' env permanently to ~./bashrc or ~./zshrc
import os
import flet as ft
def main(page: ft.Page):
prog_bar = ft.ProgressBar(value=0, bgcolor="#eeeeee", width=400)
prog_bar.visible = False
def on_dialog_result(e: ft.FilePickerResultEvent):
print("Selected files:", e.files)
upload_files()
def upload_files():
upload_list = []
if file_picker.result != None and file_picker.result.files != None:
for f in file_picker.result.files:
upload_list.append(
ft.FilePickerUploadFile(
f.name,
upload_url=page.get_upload_url(f.name, 600),
)
)
prog_bar.visible = True
page.update()
file_picker.upload(upload_list)
def on_upload_progress(e: ft.FilePickerUploadEvent):
prog_bar.value = e.progress
print(prog_bar.value)
if int(e.progress) == 1:
prog_bar.visible = False
prog_bar.update()
file_picker = ft.FilePicker(
on_result=on_dialog_result, on_upload=on_upload_progress
)
file_picker_btn = ft.ElevatedButton(
"Choose files...",
on_click=lambda _: file_picker.pick_files(
allow_multiple=False,
allowed_extensions=["mp3", "mp4"],
file_type=ft.FilePickerFileType.MEDIA,
),
)
page.overlay.append(file_picker)
page.add(ft.Row([file_picker_btn, prog_bar]))
page.update()
if not os.environ["FLET_SECRET_KEY"]:
os.environ["FLET_SECRET_KEY"] = os.urandom(12).hex()
app = ft.app(
target=main, view=ft.AppView.WEB_BROWSER, port=8550, upload_dir="assets/uploads"
)
# Setting a key is required on the server side for uploading files, and I don't know what it's for!!!
# but after set the key, now can upload file :)
` secret_key = os.getenv ( "FLET_SECRET_KEY" , default=None )
if not secret_key:
os.environ [ "FLET_SECRET_KEY" ] = os.urandom ( 12 ).hex ( )`
I want compression image when image upload server , How to do ? which method to be call when server get the file ?
I hosint my app with Cloud run FilePickerUploadFile can not upload file to webserver
Get Error:
2024-01-12 13:52:58.080 ICT Exception: Specify secret_key parameter or set FLET_SECRET_KEY environment variable to enable uploads. Open in Logs Explorer { insertId: "65a0e1ca00013a5be2a672be" labels: {1} logName: "projects/khmer-autotools/logs/run.googleapis.com%2Fstderr" receiveTimestamp: "2024-01-12T06:52:58.329130140Z" resource: {2} textPayload: "Exception: Specify secret_key parameter or set FLET_SECRET_KEY environment variable to enable uploads." timestamp: "2024-01-12T06:52:58.080475Z" }
as my code:
`import json import os import shutil
import flet_fastapi from typing import Dict
import flet from flet import ( Column, ElevatedButton, FilePicker, FilePickerResultEvent, FilePickerUploadEvent, FilePickerUploadFile, Page, ProgressRing, Ref, Row, Text, icons, )
mnt_dir = os.environ.get("MNT_DIR", "/mnt/nfs/filestore") async def main(page: Page): prog_bars: Dict[str, ProgressRing] = {} files = Ref[Column]() upload_button = Ref[ElevatedButton]()
app = flet_fastapi.app(main, upload_dir=f"uploads")
flet.app(target=main, upload_dir="uploads", view=flet.WEB_BROWSER)`