1、textbox的使用
import gradio as gr
import requests
def mobile(mobilephone):
url = 'https://api.oioweb.cn/api/common/teladress?mobile='+str(mobilephone)
headers = {}
payload = {}
response = requests.request("GET", url, headers=headers, data=payload, timeout=20)
return response.text
def textboxtest():
iface = gr.Interface(
fn=mobile,
inputs=gr.Textbox(lines=5,placeholder="请输入手机号",label='输入框'),
outputs=gr.Textbox(),
title="获取手机号码归属地")
iface.launch()
if __name__ == '__main__':
textboxtest()
2、number数值、checkout 是否选中的true or false、slider;多输入多输出
import gradio as gr
def greet(name, is_morning, temperature):
salutation = "Good morning" if is_morning else "Good evening"
greeting = f"{salutation} {name}. It is {temperature} degrees today"
celsius = (temperature - 32) * 5 / 9
return greeting, round(celsius, 2)
def testmimo(): # 用到了number数值、checkout 是否选中的true or false、slider
# 该函数有3个输入参数和2个输出参数
demo = gr.Interface(
fn=greet,
# 按照处理程序设置输入组件
inputs=["text", "checkbox", gr.Slider(0, 100)], # 注意看不用写gr.Textbox() 可以直接写textbox 之类的
# 按照处理程序设置输出组件
outputs=["text", "number"],
)
demo.launch()
if __name__ == '__main__':
testmimo()
3、radio 单选框使用
import gradio as gr
def calculator(num1, operation, num2):
if operation == "add":
return num1 + num2
elif operation == "subtract":
return num1 - num2
elif operation == "multiply":
return num1 * num2
elif operation == "divide":
return num1 / num2
def radiotest():
iface = gr.Interface(
calculator,
["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
"number",
live=True,
)
iface.launch()
if __name__ == '__main__':
radiotest()
4、checkbox多选框
import gradio as gr
def get_selection(items_):
print(f"Selected items: {items_}")
# 这里可以添加处理选中项的逻辑
return f"Selected items: {items_}"
def checkboxgroup(): # 多选框
# 定义选项列表
items = ["Item 1", "Item 2", "Item 3", "Item 4"]
# 创建一个包含多选框的界面
gr.Interface(fn=get_selection, inputs=gr.CheckboxGroup(items), outputs="text").launch()
if __name__ == '__main__':
checkboxgroup()
5、下拉框dropdown
import gradio as gr
# 下拉框更改提示
def update_dropdowns(table_colomn_1, table_colomn_2):
return "你选择的是{},{}".format(table_colomn_1,table_colomn_2)
table_colomn_1 = gr.Dropdown(["1", "2", "3", "4"], label="选项1")
table_colomn_2 = gr.Dropdown(["1", "2", "3", "4"], label="选项2")
demo = gr.Interface(update_dropdowns, inputs=[table_colomn_1, table_colomn_2], outputs='text')
demo.launch()
标签:__,控件,return,gr,python,gradio,colomn,table,def
From: https://blog.csdn.net/daxiashangxian/article/details/140469135