-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
059447c
commit 521d63e
Showing
6 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import content_uploader_app as content_uploader_app | ||
from . import uploader_app as uploader_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import base64 | ||
|
||
import mesop as me | ||
|
||
|
||
@me.stateclass | ||
class State: | ||
file: me.UploadedFile | ||
upload_count: int = 0 | ||
|
||
|
||
@me.page(path="/components/uploader/e2e/content_uploader_app") | ||
def app(): | ||
state = me.state(State) | ||
with me.box(style=me.Style(padding=me.Padding.all(15))): | ||
with me.content_uploader( | ||
accepted_file_types=["image/jpeg", "image/png"], | ||
on_upload=handle_upload, | ||
type="flat", | ||
color="primary", | ||
style=me.Style(font_weight="bold"), | ||
): | ||
with me.box(style=me.Style(display="flex", gap=5)): | ||
me.icon("upload") | ||
me.text("Upload Image", style=me.Style(line_height="25px")) | ||
|
||
if state.file.size: | ||
with me.box(style=me.Style(margin=me.Margin.all(10))): | ||
me.text(f"File name: {state.file.name}") | ||
me.text(f"File size: {state.file.size}") | ||
me.text(f"File type: {state.file.mime_type}") | ||
me.text(f"Upload count: {state.upload_count}") | ||
|
||
with me.box(style=me.Style(margin=me.Margin.all(10))): | ||
me.image(src=_convert_contents_data_url(state.file)) | ||
|
||
|
||
def handle_upload(event: me.UploadEvent): | ||
state = me.state(State) | ||
state.file = event.file | ||
state.upload_count += 1 | ||
|
||
|
||
def _convert_contents_data_url(file: me.UploadedFile) -> str: | ||
return ( | ||
f"data:{file.mime_type};base64,{base64.b64encode(file.getvalue()).decode()}" | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters