Spaces:
Runtime error
Runtime error
import gradio as gr | |
# 定义一个全局变量,用于存储按钮的点击状态 | |
button_clicked = False | |
# 创建一个处理函数,该函数在按钮点击时被调用 | |
def button_click(): | |
global button_clicked | |
button_clicked = True | |
def dynamic_buttons(user_input): | |
if user_input == "A": | |
button_html = "<button onclick='button_click()'>点击我</button>" | |
elif user_input == "B": | |
button_html = "<button onclick='alert(\"按钮B被点击了!\")'>按钮B</button>" | |
else: | |
button_html = "<button onclick='alert(\"未知按钮被点击了!\")'>未知按钮</button>" | |
return button_html | |
# 创建一个Gradio界面 | |
iface = gr.Interface( | |
fn=dynamic_buttons, | |
inputs="text", | |
outputs="html", | |
live=False # 禁用自动更新,因为我们将使用JavaScript来处理 | |
) | |
# 设置HTML内容,包括一个按钮 | |
# button_html = "<button onclick='button_click()'>点击我</button>" | |
# iface.update(button_html) | |
# 启动Gradio界面 | |
iface.launch() | |
# 监听按钮的点击状态 | |
while True: | |
if button_clicked: | |
# 按钮被点击,运行您的Python函数 | |
# 这里可以添加您的代码来创建新的Gradio按钮或执行其他操作 | |
print("按钮A被点击了!") | |
button_clicked = False # 重置按钮点击状态 | |