gradio/demo/state_change/run.py
pngwn 9d2d6051ca
Change client submit API to be an AsyncIterable and support more platforms (#8451)
* fix param name

* format

* save

* changes

* changes

* fix param name

* format

* switch to async iterable interface

* switch to async iterable interface

* changes

* add changeset

* fix

* fix param name

* format

* fixes

* fix checks

* fix checks

* add changeset

* fix checks

* add changeset

* add changeset

* fix checks

* fix param name

* format

* fix types

* cleanup comments

* add changeset

* fix param name

* format

* changes

* Refactor Cancelling Logic To Use /cancel (#8370)

* Cancel refactor

* add changeset

* add changeset

* types

* Add code

* Fix types

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* fix param name

* format

* changes

* fix

* fix param name

* format

* fix test

* fix notebooks

* fix type

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Freddy Boulton <alfonsoboulton@gmail.com>
2024-06-06 13:16:14 +01:00

66 lines
2.0 KiB
Python

import gradio as gr
with gr.Blocks() as demo:
with gr.Row():
state_a = gr.State(0)
btn_a = gr.Button("Increment A")
value_a = gr.Number(label="Number A")
btn_a.click(lambda x: x + 1, state_a, state_a)
state_a.change(lambda x: x, state_a, value_a)
with gr.Row():
state_b = gr.State(0)
btn_b = gr.Button("Increment B")
value_b = gr.Number(label="Number B")
btn_b.click(lambda x: x + 1, state_b, state_b)
@gr.on(inputs=state_b, outputs=value_b)
def identity(x):
return x
@gr.render(inputs=[state_a, state_b])
def render(a, b):
for x in range(a):
with gr.Row():
for y in range(b):
gr.Button(f"Button {x}, {y}")
list_state = gr.State([])
dict_state = gr.State(dict())
nested_list_state = gr.State([])
set_state = gr.State(set())
def transform_list(x):
return {n: n for n in x}, [x[:] for _ in range(len(x))], set(x)
list_state.change(
transform_list,
inputs=list_state,
outputs=[dict_state, nested_list_state, set_state],
)
all_textbox = gr.Textbox(label="Output")
click_count = gr.Number(label="Clicks")
change_count = gr.Number(label="Changes")
gr.on(
inputs=[change_count, dict_state, nested_list_state, set_state],
triggers=[dict_state.change, nested_list_state.change, set_state.change],
fn=lambda x, *args: (x + 1, "\n".join(str(arg) for arg in args)),
outputs=[change_count, all_textbox],
)
count_to_3_btn = gr.Button("Count to 3")
count_to_3_btn.click(lambda: [1, 2, 3], outputs=list_state)
zero_all_btn = gr.Button("Zero All")
zero_all_btn.click(lambda x: [0] * len(x), inputs=list_state, outputs=list_state)
gr.on(
[count_to_3_btn.click, zero_all_btn.click],
lambda x: x + 1,
click_count,
click_count,
)
if __name__ == "__main__":
demo.launch()