|
| 1 | +from typing import Callable |
| 2 | + |
| 3 | +import mesop as me |
| 4 | + |
| 5 | + |
| 6 | +@me.stateclass |
| 7 | +class State: |
| 8 | + input: str |
| 9 | + output: str |
| 10 | + textarea_key: int |
| 11 | + |
| 12 | + |
| 13 | +def text_to_image( |
| 14 | + transform: Callable[[str], str], |
| 15 | + *, |
| 16 | + title: str | None = None, |
| 17 | +): |
| 18 | + """Creates a simple UI which takes in a text input and returns an image output. |
| 19 | +
|
| 20 | + This function creates event handlers for text input and output operations |
| 21 | + using the provided function `transform` to process the input and generate the image |
| 22 | + output. |
| 23 | +
|
| 24 | + Args: |
| 25 | + transform: Function that takes in a string input and returns a URL to an image or a base64 encoded image. |
| 26 | + title: Headline text to display at the top of the UI. |
| 27 | + """ |
| 28 | + |
| 29 | + def on_input(e: me.InputEvent): |
| 30 | + state = me.state(State) |
| 31 | + state.input = e.value |
| 32 | + |
| 33 | + def on_click_generate(e: me.ClickEvent): |
| 34 | + state = me.state(State) |
| 35 | + state.output = transform(state.input) |
| 36 | + |
| 37 | + def on_click_clear(e: me.ClickEvent): |
| 38 | + state = me.state(State) |
| 39 | + state.input = "" |
| 40 | + state.output = "" |
| 41 | + state.textarea_key += 1 |
| 42 | + |
| 43 | + with me.box( |
| 44 | + style=me.Style( |
| 45 | + background="#f0f4f8", |
| 46 | + height="100%", |
| 47 | + ) |
| 48 | + ): |
| 49 | + with me.box( |
| 50 | + style=me.Style( |
| 51 | + background="#f0f4f8", |
| 52 | + padding=me.Padding(top=24, left=24, right=24, bottom=24), |
| 53 | + display="flex", |
| 54 | + flex_direction="column", |
| 55 | + ) |
| 56 | + ): |
| 57 | + if title: |
| 58 | + me.text(title, type="headline-5") |
| 59 | + with me.box( |
| 60 | + style=me.Style( |
| 61 | + margin=me.Margin(left="auto", right="auto"), |
| 62 | + width="min(1024px, 100%)", |
| 63 | + gap="24px", |
| 64 | + flex_grow=1, |
| 65 | + display="flex", |
| 66 | + flex_wrap="wrap", |
| 67 | + ) |
| 68 | + ): |
| 69 | + box_style = me.Style( |
| 70 | + flex_basis="max(480px, calc(50% - 48px))", |
| 71 | + background="#fff", |
| 72 | + border_radius=12, |
| 73 | + box_shadow=( |
| 74 | + "0 3px 1px -2px #0003, 0 2px 2px #00000024, 0 1px 5px #0000001f" |
| 75 | + ), |
| 76 | + padding=me.Padding(top=16, left=16, right=16, bottom=16), |
| 77 | + display="flex", |
| 78 | + flex_direction="column", |
| 79 | + ) |
| 80 | + |
| 81 | + with me.box(style=box_style): |
| 82 | + me.text("Input", style=me.Style(font_weight=500)) |
| 83 | + me.box(style=me.Style(height=16)) |
| 84 | + me.textarea( |
| 85 | + key=str(me.state(State).textarea_key), |
| 86 | + on_input=on_input, |
| 87 | + rows=5, |
| 88 | + autosize=True, |
| 89 | + max_rows=15, |
| 90 | + style=me.Style(width="100%"), |
| 91 | + ) |
| 92 | + me.box(style=me.Style(height=12)) |
| 93 | + with me.box( |
| 94 | + style=me.Style(display="flex", justify_content="space-between") |
| 95 | + ): |
| 96 | + me.button( |
| 97 | + "Clear", |
| 98 | + color="primary", |
| 99 | + type="stroked", |
| 100 | + on_click=on_click_clear, |
| 101 | + ) |
| 102 | + me.button( |
| 103 | + "Generate", |
| 104 | + color="primary", |
| 105 | + type="flat", |
| 106 | + on_click=on_click_generate, |
| 107 | + ) |
| 108 | + with me.box(style=box_style): |
| 109 | + me.text("Output", style=me.Style(font_weight=500)) |
| 110 | + if me.state(State).output: |
| 111 | + with me.box( |
| 112 | + style=me.Style( |
| 113 | + display="grid", |
| 114 | + justify_content="center", |
| 115 | + justify_items="center", |
| 116 | + ) |
| 117 | + ): |
| 118 | + me.image( |
| 119 | + src=me.state(State).output, |
| 120 | + style=me.Style(width="100%", margin=me.Margin(top=10)), |
| 121 | + ) |
0 commit comments