changed callback to be async

This commit is contained in:
2025-09-27 08:26:37 +02:00
parent 8034c9ec11
commit 20c95a384e
2 changed files with 11 additions and 11 deletions

View File

@@ -14,7 +14,7 @@ async def main_page():
# Example 1: Multiple files with content
uploaded_files = ui.column().classes('w-full mt-4')
def handle_multiple_files(file: FileData):
async def handle_multiple_files(file: FileData):
with uploaded_files:
with ui.card().classes('p-2 mb-2'):
ui.label(f"📄 {file['name']}").classes('font-medium')
@@ -26,7 +26,7 @@ async def main_page():
on_upload=handle_multiple_files,
multiple=True,
accept='.pdf,.docx,.txt,.jpg,.png',
max_size=5 # 5MB limit
max_size=15 # 5MB limit
).classes('w-full max-w-xl mx-auto')
ui.separator().classes('my-8')
@@ -72,7 +72,7 @@ async def main_page():
# Example 4: Image drop with preview
image_preview = ui.column().classes('w-full mt-4')
def handle_image(img: ImageData):
async def handle_image(img: ImageData):
print(f"Image uploaded: {img['image'].format}, Size: {img['size']}")
image_preview.clear()
with image_preview:

View File

@@ -108,7 +108,7 @@ class FileDrop(ui.element):
}})();
''')
def _handle_upload(self, e):
async def _handle_upload(self, e):
"""Handle uploaded files"""
if not self.on_upload_callback:
return
@@ -116,12 +116,12 @@ class FileDrop(ui.element):
# Process single file upload
if hasattr(e, 'content'):
print('content')
file_data = self._process_file(e)
file_data = await self._process_file(e)
if file_data:
# For single file mode, return the dict directly
self.on_upload_callback(file_data)
await self.on_upload_callback(file_data)
def _process_file(self, file_obj) -> FileData | None:
async def _process_file(self, file_obj) -> FileData | None:
"""Process a single file object"""
if not hasattr(file_obj, 'content'):
return None
@@ -175,19 +175,19 @@ class ImageDrop(FileDrop):
*args, **kwargs
)
def _handle_image_upload(self, data: FileData):
async def _handle_image_upload(self, data: FileData):
"""Convert file data to PIL Images before calling user callback"""
if self._user_callback:
if isinstance(data, list) and len(data) == 1:
img = Image.open(io.BytesIO(data[0]['content']))
self._user_callback(img)
await self._user_callback(img)
elif isinstance(data, list):
# Multiple images - convert each to PIL Image
images = []
for file_data in data:
img = Image.open(io.BytesIO(file_data['content']))
images.append(img)
self._user_callback(images)
await self._user_callback(images)
else:
# Single image - convert to PIL Image
image_data: ImageData = {'name': data['name'],
@@ -195,4 +195,4 @@ class ImageDrop(FileDrop):
'file_type': data['file_type'],
'image': Image.open(io.BytesIO(data['content']))}
# img = Image.open(io.BytesIO(data['content']))
self._user_callback(image_data)
await self._user_callback(image_data)