Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-to committed Oct 29, 2024
1 parent 059447c commit 521d63e
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
1 change: 1 addition & 0 deletions mesop/components/uploader/e2e/__init__.py
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
47 changes: 47 additions & 0 deletions mesop/components/uploader/e2e/content_uploader_app.py
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()}"
)
32 changes: 32 additions & 0 deletions mesop/components/uploader/e2e/content_uploader_test.ts

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions mesop/components/uploader/e2e/uploader_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ class State:
def app():
state = me.state(State)
with me.box(style=me.Style(padding=me.Padding.all(15))):
with me.content_uploader(
me.uploader(
label="Upload Image",
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))):
Expand Down
4 changes: 1 addition & 3 deletions mesop/components/uploader/e2e/uploader_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ test('test upload file', async ({page}) => {
).toHaveCount(1);

// Check that we can re-upload the same file.
// Also check that the icon in the button is being rendered in the composite
// uploader component.
await page.locator('//*[@role="img" and text()=" upload"]').click();
await page.getByText('Upload Image').click();
const fileChooser2 = await fileChooserPromise;
await fileChooser2.setFiles(path.join(__dirname, 'mesop_robot.jpeg'));
await expect(page.getByText('File name: mesop_robot.jpeg')).toHaveCount(1);
Expand Down
2 changes: 1 addition & 1 deletion mesop/components/uploader/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def uploader(
accepted_file_types: Sequence[str] | None = None,
key: str | None = None,
on_upload: Callable[[UploadEvent], Any] | None = None,
type: Literal["raised", "flat", "stroked", "icon"] | None = None,
type: Literal["raised", "flat", "stroked"] | None = None,
color: Literal["primary", "accent", "warn"] | None = None,
disable_ripple: bool = False,
disabled: bool = False,
Expand Down

0 comments on commit 521d63e

Please sign in to comment.