File size: 1,397 Bytes
869c384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

import gradio as gr
import random
import time
# 动态生成 HTML 和 JavaScript 代码
def generate_button_html():
#     button_html = f"""
#     <script>         function show() {{
# 		// 在这里执行 JavaScript 代码
# 		alert("Button clicked!");
# 		// 隐藏按钮
# 		var button = document.getElementById('my-button2');
# 		button.style.display = 'none';
# 	}}
# </script>
# <button id="my-button" onclick="show();">Click Me</button>
# <button id="my-button2" onclick="show();">Click Me2</button>
#     """
    button_html = f"""
    <script>  alert("Button clicked!")  </script>
    """
    button_html = button_html.replace("\n","<br>")
    print(button_html)
    return button_html

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    btn = gr.Button(value="Submit")
    btn.visible = False
    msg = gr.Textbox()
    clear = gr.ClearButton([msg, chatbot])

    def respond(message, chat_history):
        # bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
        bot_message = f"Hello! Click the button below:<br>{generate_button_html()}"
        chat_history.append((message, bot_message))
        time.sleep(2)

        btn.visible = True
        gr.update(value="", interactive=True)
        return "", chat_history,btn

    msg.submit(respond, [msg, chatbot], [msg, chatbot])

if __name__ == "__main__":
    demo.launch()