File size: 4,085 Bytes
8d2d17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edf8269
8d2d17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa1c91b
 
 
8d2d17c
 
 
aa1c91b
8d2d17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e024e
8d2d17c
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# %%
import gradio as gr

TEST_VIDEO_A = "mp4/a.mp4"
TEST_VIDEO_B = "mp4/b.mp4"

TEST_IMAGE_A = "img/a.jpg"
TEST_IMAGE_B = "img/b.jpg"

def alert_change(component, value):
    print(f"Detected {component} change, {type(value)}")

    if type(value) == list or type(value) == str:
        print(value)

def change_interactive(state):
    return gr.Video(interactive=not state), not state

with gr.Blocks() as demo:
    with gr.Tab(label="Text change"):
        with gr.Row():
            with gr.Column():
                textbox1 = gr.Textbox()
                textbox2 = gr.Textbox(interactive=True)

            with gr.Column():
                btn = gr.Button()

        def btn_click(state):
            return state

        def text_change(value):
            print("text_change", value)

        btn.click(fn=btn_click, inputs=textbox1, outputs=textbox2)
        textbox2.change(fn=alert_change, inputs=[gr.State("Text"), textbox2])

    with gr.Tab(label="Video change, play, pause"):
        with gr.Row():
            with gr.Column():
                radio1 = gr.Radio(
                    choices=[TEST_VIDEO_A, TEST_VIDEO_B],
                    interactive=True,
                    type="index",
                )

                video_btn = gr.Button("Change interactive")

            with gr.Column():
                video1 = gr.Video(value=TEST_VIDEO_A, interactive=False)
                video1_interactive = gr.State(value=False)

        def change_video(index):
            if index == 0:
                return TEST_VIDEO_A
            elif index == 1:
                return TEST_VIDEO_B

        def video_play():
            print("video_play")

        def video_pause():
            print("video_pause")

        def video_stop():
            print("video_stop")

        def video_end():
            print("video_end")

        video1.play(fn=video_play)
        video1.pause(fn=video_pause)
        video1.stop(fn=video_stop)
        video1.end(fn=video_end)

        radio1.change(fn=change_video, inputs=radio1, outputs=video1)
        video1.change(fn=alert_change, inputs=[gr.State("Video"), video1])

        video_btn.click(
            fn=change_interactive,
            inputs=video1_interactive,
            outputs=[video1, video1_interactive],
        )

    with gr.Tab(label="Image change"):
        with gr.Row():
            with gr.Column():
                radio2 = gr.Radio(
                    choices=[TEST_IMAGE_A, TEST_IMAGE_B],
                    interactive=True,
                    type="index",
                )

            with gr.Column():
                image1 = gr.Image(value=TEST_IMAGE_A, interactive=True)

        def change_image(index):
            if index == 0:
                return TEST_IMAGE_A
            elif index == 1:
                return TEST_IMAGE_B

        radio2.change(fn=change_image, inputs=radio2, outputs=image1)
        image1.change(fn=alert_change, inputs=[gr.State("Image"), image1])

    with gr.Tab(label="File"):
        with gr.Row():
            with gr.Column():
                radio3 = gr.Radio(
                    choices=["A", "B", "AB"],
                    interactive=True,
                    type="index",
                )

                file_btn = gr.Button("Change interactive")

            with gr.Column():
                file1 = gr.File(
                    value=[TEST_IMAGE_A, TEST_IMAGE_B],
                    interactive=False,
                    file_count="multiple",
                )
                file1_interactive = gr.State(value=False)

        def change_file(index):
            if index == 0 or index == 1:
                return [TEST_IMAGE_A]
            elif index == 2:
                return [TEST_IMAGE_A, TEST_IMAGE_B]

        radio3.change(fn=change_file, inputs=radio3, outputs=file1)
        file1.change(fn=alert_change, inputs=[gr.State("File"), file1])

        file_btn.click(
            fn=change_interactive,
            inputs=file1_interactive,
            outputs=[file1, file1_interactive],
        )

demo.launch()