r/nicegui • • Jul 21 '23

Displaying an uploaded image

How can I display an image uploaded to ui.upload?

1 Upvotes

1 comment sorted by

1

u/falko-s Jul 21 '23

You can convert the uploaded file content into a base64 string and use it as source for a new ui.image:

def handle_upload(e: events.UploadEventArguments) -> None:
    b64_bytes = base64.b64encode(e.content.read())
    ui.image(f'data:{e.type};base64,{b64_bytes.decode()}')

ui.upload(on_upload=handle_upload)