changed callback to be async
This commit is contained in:
@@ -14,7 +14,7 @@ async def main_page():
|
|||||||
# Example 1: Multiple files with content
|
# Example 1: Multiple files with content
|
||||||
uploaded_files = ui.column().classes('w-full mt-4')
|
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 uploaded_files:
|
||||||
with ui.card().classes('p-2 mb-2'):
|
with ui.card().classes('p-2 mb-2'):
|
||||||
ui.label(f"📄 {file['name']}").classes('font-medium')
|
ui.label(f"📄 {file['name']}").classes('font-medium')
|
||||||
@@ -26,7 +26,7 @@ async def main_page():
|
|||||||
on_upload=handle_multiple_files,
|
on_upload=handle_multiple_files,
|
||||||
multiple=True,
|
multiple=True,
|
||||||
accept='.pdf,.docx,.txt,.jpg,.png',
|
accept='.pdf,.docx,.txt,.jpg,.png',
|
||||||
max_size=5 # 5MB limit
|
max_size=15 # 5MB limit
|
||||||
).classes('w-full max-w-xl mx-auto')
|
).classes('w-full max-w-xl mx-auto')
|
||||||
|
|
||||||
ui.separator().classes('my-8')
|
ui.separator().classes('my-8')
|
||||||
@@ -72,7 +72,7 @@ async def main_page():
|
|||||||
# Example 4: Image drop with preview
|
# Example 4: Image drop with preview
|
||||||
image_preview = ui.column().classes('w-full mt-4')
|
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']}")
|
print(f"Image uploaded: {img['image'].format}, Size: {img['size']}")
|
||||||
image_preview.clear()
|
image_preview.clear()
|
||||||
with image_preview:
|
with image_preview:
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class FileDrop(ui.element):
|
|||||||
}})();
|
}})();
|
||||||
''')
|
''')
|
||||||
|
|
||||||
def _handle_upload(self, e):
|
async def _handle_upload(self, e):
|
||||||
"""Handle uploaded files"""
|
"""Handle uploaded files"""
|
||||||
if not self.on_upload_callback:
|
if not self.on_upload_callback:
|
||||||
return
|
return
|
||||||
@@ -116,12 +116,12 @@ class FileDrop(ui.element):
|
|||||||
# Process single file upload
|
# Process single file upload
|
||||||
if hasattr(e, 'content'):
|
if hasattr(e, 'content'):
|
||||||
print('content')
|
print('content')
|
||||||
file_data = self._process_file(e)
|
file_data = await self._process_file(e)
|
||||||
if file_data:
|
if file_data:
|
||||||
# For single file mode, return the dict directly
|
# 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"""
|
"""Process a single file object"""
|
||||||
if not hasattr(file_obj, 'content'):
|
if not hasattr(file_obj, 'content'):
|
||||||
return None
|
return None
|
||||||
@@ -175,19 +175,19 @@ class ImageDrop(FileDrop):
|
|||||||
*args, **kwargs
|
*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"""
|
"""Convert file data to PIL Images before calling user callback"""
|
||||||
if self._user_callback:
|
if self._user_callback:
|
||||||
if isinstance(data, list) and len(data) == 1:
|
if isinstance(data, list) and len(data) == 1:
|
||||||
img = Image.open(io.BytesIO(data[0]['content']))
|
img = Image.open(io.BytesIO(data[0]['content']))
|
||||||
self._user_callback(img)
|
await self._user_callback(img)
|
||||||
elif isinstance(data, list):
|
elif isinstance(data, list):
|
||||||
# Multiple images - convert each to PIL Image
|
# Multiple images - convert each to PIL Image
|
||||||
images = []
|
images = []
|
||||||
for file_data in data:
|
for file_data in data:
|
||||||
img = Image.open(io.BytesIO(file_data['content']))
|
img = Image.open(io.BytesIO(file_data['content']))
|
||||||
images.append(img)
|
images.append(img)
|
||||||
self._user_callback(images)
|
await self._user_callback(images)
|
||||||
else:
|
else:
|
||||||
# Single image - convert to PIL Image
|
# Single image - convert to PIL Image
|
||||||
image_data: ImageData = {'name': data['name'],
|
image_data: ImageData = {'name': data['name'],
|
||||||
@@ -195,4 +195,4 @@ class ImageDrop(FileDrop):
|
|||||||
'file_type': data['file_type'],
|
'file_type': data['file_type'],
|
||||||
'image': Image.open(io.BytesIO(data['content']))}
|
'image': Image.open(io.BytesIO(data['content']))}
|
||||||
# img = Image.open(io.BytesIO(data['content']))
|
# img = Image.open(io.BytesIO(data['content']))
|
||||||
self._user_callback(image_data)
|
await self._user_callback(image_data)
|
||||||
|
|||||||
Reference in New Issue
Block a user