Example #1
0
def ArtistList():
    artist_to_add, set_artist_to_add = use_state("")
    artists, set_artists = use_state(
        ["Marta Colvin Andrade", "Lamidi Olonade Fakeye", "Louise Nevelson"])

    def handle_change(event):
        set_artist_to_add(event["target"]["value"])

    def handle_add_click(event):
        if artist_to_add not in artists:
            set_artists([*artists, artist_to_add])
            set_artist_to_add("")

    def make_handle_delete_click(index):
        def handle_click(event):
            set_artists(artists[:index] + artists[index + 1:])

        return handle_click

    return html.div(
        html.h1("Inspiring sculptors:"),
        html.input({
            "value": artist_to_add,
            "onChange": handle_change
        }),
        html.button({"onClick": handle_add_click}, "add"),
        html.ul([
            html.li(
                name,
                html.button({"onClick": make_handle_delete_click(index)},
                            "delete"),
                key=name,
            ) for index, name in enumerate(artists)
        ]),
    )
Example #2
0
def App():
    is_sent, set_is_sent = use_state(False)
    message, set_message = use_state("")

    if is_sent:
        return html.div(
            html.h1("Message sent!"),
            html.button(
                {"onClick": lambda event: set_is_sent(False)}, "Send new message?"
            ),
        )

    @event(prevent_default=True)
    def handle_submit(event):
        set_message("")
        set_is_sent(True)

    return html.form(
        {"onSubmit": handle_submit, "style": {"display": "inline-grid"}},
        html.textarea(
            {
                "placeholder": "Your message here...",
                "value": message,
                "onChange": lambda event: set_message(event["target"]["value"]),
            }
        ),
        html.button({"type": "submit"}, "Send"),
    )
Example #3
0
def ColorButton():
    color, set_color = use_state("gray")

    def handle_click(event):
        set_color("orange")
        set_color("pink")
        set_color("blue")

    def handle_reset(event):
        set_color("gray")

    return html.div(
        html.button(
            {
                "onClick": handle_click,
                "style": {
                    "backgroundColor": color
                }
            }, "Set Color"),
        html.button(
            {
                "onClick": handle_reset,
                "style": {
                    "backgroundColor": color
                }
            }, "Reset"),
    )
Example #4
0
def Definitions():
    term_to_add, set_term_to_add = use_state(None)
    definition_to_add, set_definition_to_add = use_state(None)
    all_terms, set_all_terms = use_state({})

    def handle_term_to_add_change(event):
        set_term_to_add(event["target"]["value"])

    def handle_definition_to_add_change(event):
        set_definition_to_add(event["target"]["value"])

    def handle_add_click(event):
        if term_to_add and definition_to_add:
            set_all_terms({**all_terms, term_to_add: definition_to_add})
            set_term_to_add(None)
            set_definition_to_add(None)

    def make_delete_click_handler(term_to_delete):
        def handle_click(event):
            set_all_terms({t: d for t, d in all_terms.items() if t != term_to_delete})

        return handle_click

    return html.div(
        html.button({"onClick": handle_add_click}, "add term"),
        html.label(
            "Term: ",
            html.input({"value": term_to_add, "onChange": handle_term_to_add_change}),
        ),
        html.label(
            "Definition: ",
            html.input(
                {
                    "value": definition_to_add,
                    "onChange": handle_definition_to_add_change,
                }
            ),
        ),
        html.hr(),
        [
            html.div(
                html.button(
                    {"onClick": make_delete_click_handler(term)}, "delete term"
                ),
                html.dt(term),
                html.dd(definition),
                key=term,
            )
            for term, definition in all_terms.items()
        ],
    )
Example #5
0
def Gallery():
    index, set_index = hooks.use_state(0)

    def handle_click(event):
        set_index(index + 1)

    bounded_index = index % len(sculpture_data)
    sculpture = sculpture_data[bounded_index]
    alt = sculpture["alt"]
    artist = sculpture["artist"]
    description = sculpture["description"]
    name = sculpture["name"]
    url = sculpture["url"]

    return html.div(
        html.button({"onClick": handle_click}, "Next"),
        html.h2(name, " by ", artist),
        html.p(f"({bounded_index + 1} of {len(sculpture_data)})"),
        html.img({
            "src": url,
            "alt": alt,
            "style": {
                "height": "200px"
            }
        }),
        html.p(description),
    )
Example #6
0
 def Root():
     toggle, toggle_type.current = use_toggle(True)
     handler = element_static_handler.use(lambda: None)
     if toggle:
         return html.div(html.button({"onEvent": handler}))
     else:
         return html.div(SomeComponent())
Example #7
0
def ArtistList():
    artists, set_artists = use_state(
        ["Marta Colvin Andrade", "Lamidi Olonade Fakeye", "Louise Nevelson"])

    def handle_sort_click(event):
        set_artists(list(sorted(artists)))

    def handle_reverse_click(event):
        set_artists(list(reversed(artists)))

    return html.div(
        html.h1("Inspiring sculptors:"),
        html.button({"onClick": handle_sort_click}, "sort"),
        html.button({"onClick": handle_reverse_click}, "reverse"),
        html.ul([html.li(name, key=name) for name in artists]),
    )
Example #8
0
def Counter():
    number, set_number = use_state(0)

    def handle_click(event):
        set_number(number + 5)
        print(number)

    return html.div(
        html.h1(number),
        html.button({"onClick": handle_click}, "Increment"),
    )
Example #9
0
def Counter():
    number, set_number = use_state(0)

    async def handle_click(event):
        await asyncio.sleep(3)
        set_number(lambda old_number: old_number + 1)

    return html.div(
        html.h1(number),
        html.button({"onClick": handle_click}, "Increment"),
    )
Example #10
0
def Scene():
    position, set_position = use_state(Position(100, 100, 0))

    return html.div(
        {"style": {"width": "225px"}},
        html.div(
            {
                "style": {
                    "width": "200px",
                    "height": "200px",
                    "backgroundColor": "slategray",
                }
            },
            image(
                "png",
                CHARACTER_IMAGE,
                {
                    "style": {
                        "position": "relative",
                        "left": f"{position.x}px",
                        "top": f"{position.y}.px",
                        "transform": f"rotate({position.angle}deg) scale(2, 2)",
                    }
                },
            ),
        ),
        html.button({"onClick": lambda e: set_position(translate(x=-10))}, "Move Left"),
        html.button({"onClick": lambda e: set_position(translate(x=10))}, "Move Right"),
        html.button({"onClick": lambda e: set_position(translate(y=-10))}, "Move Up"),
        html.button({"onClick": lambda e: set_position(translate(y=10))}, "Move Down"),
        html.button({"onClick": lambda e: set_position(rotate(-30))}, "Rotate Left"),
        html.button({"onClick": lambda e: set_position(rotate(30))}, "Rotate Right"),
    )
Example #11
0
def Counter():
    number, set_number = use_state(0)

    async def handle_click(event):
        set_number(number + 5)
        print("about to print...")
        await asyncio.sleep(3)
        print(number)

    return html.div(
        html.h1(number),
        html.button({"onClick": handle_click}, "Increment"),
    )
Example #12
0
 def Parent():
     state, set_state = use_state(0)
     return html.div(
         html.button(
             {
                 "onClick":
                 set_child_key_num.use(lambda: set_state(state + 1))
             },
             "click me",
         ),
         Child("some-key"),
         Child(f"key-{state}"),
     )
Example #13
0
def Gallery():
    index, set_index = hooks.use_state(0)
    show_more, set_show_more = hooks.use_state(False)

    def handle_next_click(event):
        set_index(index + 1)

    def handle_more_click(event):
        set_show_more(not show_more)

    bounded_index = index % len(sculpture_data)
    sculpture = sculpture_data[bounded_index]
    alt = sculpture["alt"]
    artist = sculpture["artist"]
    description = sculpture["description"]
    name = sculpture["name"]
    url = sculpture["url"]

    return html.div(
        html.button({"onClick": handle_next_click}, "Next"),
        html.h2(name, " by ", artist),
        html.p(f"({bounded_index + 1} or {len(sculpture_data)})"),
        html.img({
            "src": url,
            "alt": alt,
            "style": {
                "height": "200px"
            }
        }),
        html.div(
            html.button(
                {"onClick": handle_more_click},
                f"{'Show' if show_more else 'Hide'} details",
            ),
            (html.p(description) if show_more else ""),
        ),
    )
Example #14
0
def App():
    recipient, set_recipient = use_state("Alice")
    message, set_message = use_state("")

    @event(prevent_default=True)
    async def handle_submit(event):
        set_message("")
        print("About to send message...")
        await asyncio.sleep(5)
        print(f"Sent '{message}' to {recipient}")

    return html.form(
        {
            "onSubmit": handle_submit,
            "style": {
                "display": "inline-grid"
            }
        },
        html.label(
            "To: ",
            html.select(
                {
                    "value":
                    recipient,
                    "onChange":
                    lambda event: set_recipient(event["target"]["value"]),
                },
                html.option({"value": "Alice"}, "Alice"),
                html.option({"value": "Bob"}, "Bob"),
            ),
        ),
        html.input({
            "type":
            "text",
            "placeholder":
            "Your message...",
            "value":
            message,
            "onChange":
            lambda event: set_message(event["target"]["value"]),
        }),
        html.button({"type": "submit"}, "Send"),
    )
Example #15
0
def DivInDiv():
    stop_propagatation, set_stop_propagatation = hooks.use_state(True)
    inner_count, set_inner_count = hooks.use_state(0)
    outer_count, set_outer_count = hooks.use_state(0)

    div_in_div = html.div(
        {
            "onClick": lambda event: set_outer_count(outer_count + 1),
            "style": {
                "height": "100px",
                "width": "100px",
                "backgroundColor": "red"
            },
        },
        html.div(
            {
                "onClick":
                event(
                    lambda event: set_inner_count(inner_count + 1),
                    stop_propagation=stop_propagatation,
                ),
                "style": {
                    "height": "50px",
                    "width": "50px",
                    "backgroundColor": "blue"
                },
            }, ),
    )

    return html.div(
        html.button(
            {
                "onClick":
                lambda event: set_stop_propagatation(not stop_propagatation)
            },
            "Toggle Propogation",
        ),
        html.pre(f"Will propagate: {not stop_propagatation}"),
        html.pre(f"Inner click count: {inner_count}"),
        html.pre(f"Outer click count: {outer_count}"),
        div_in_div,
    )
Example #16
0
def CounterList():
    counters, set_counters = use_state([0, 0, 0])

    def make_increment_click_handler(index):
        def handle_click(event):
            new_value = counters[index] + 1
            set_counters(counters[:index] + [new_value] + counters[index + 1 :])

        return handle_click

    return html.ul(
        [
            html.li(
                count,
                html.button({"onClick": make_increment_click_handler(index)}, "+1"),
                key=index,
            )
            for index, count in enumerate(counters)
        ]
    )
Example #17
0
def ArtistList():
    artist_to_add, set_artist_to_add = use_state("")
    artists, set_artists = use_state([])

    def handle_change(event):
        set_artist_to_add(event["target"]["value"])

    def handle_click(event):
        if artist_to_add and artist_to_add not in artists:
            set_artists([*artists, artist_to_add])
            set_artist_to_add("")

    return html.div(
        html.h1("Inspiring sculptors:"),
        html.input({
            "value": artist_to_add,
            "onChange": handle_change
        }),
        html.button({"onClick": handle_click}, "add"),
        html.ul([html.li(name, key=name) for name in artists]),
    )
Example #18
0
 def Root():
     event_name, set_event_name.current = use_state("first")
     return html.button({
         event_name:
         event_handler.use(lambda: did_trigger.set_current(True))
     })
Example #19
0
 def SomeComponent():
     handler = component_static_handler.use(lambda: None)
     return html.button({"onAnotherEvent": handler})
Example #20
0
def Button():
    return html.button("I don't do anything yet")
Example #21
0
def PrintButton(display_text, message_text):
    def handle_event(event):
        print(message_text)

    return html.button({"onClick": handle_event}, display_text)
Example #22
0
def ButtonWithDelay(message, delay):
    async def handle_event(event):
        await asyncio.sleep(delay)
        print(message)

    return html.button({"onClick": handle_event}, message)
Example #23
0
def Button():
    def handle_event(event):
        print(event)

    return html.button({"onClick": handle_event}, "Click me!")
Example #24
0
def Button(display_text, on_click):
    return html.button({"onClick": on_click}, display_text)