Gradio
用Gradio实现一个调用一个REST API,访问的的API地址是http://10.211.6.6:8081/gelf,参数a=1,b=2,返回字段有3个field1, field2, field3, 三个字符的类型都是字符串类型,返回3条数据,结构如下:
filed1, filed2,field3
a,b,c,
d,e,f
h,i,j
用Gradio交结果分页显出来,用二维表格的形式显示, 给同具体体Gradio实现的Python代码
下面是使用Gradio实现的Python代码:
import gradio as gr
import requests
def rest_api_call():
url = "http://10.211.6.6:8081/gelf"
params = {"a": 1, "b": 2}
response = requests.get(url, params=params)
data = response.json()
return data
def display_results(page):
data = rest_api_call()
start_index = (page - 1) * 3
end_index = page * 3
results = data[start_index:end_index]
table_data = [["field1", "field2", "field3"]]
for result in results:
table_data.append([result["field1"], result["field2"], result["field3"]])
return table_data
inputs = gr.inputs.Number(label="Page", default=1, minimum=1, maximum=2)
outputs = gr.outputs.Table(header=["field1", "field2", "field3"])
interface = gr.Interface(fn=display_results, inputs=inputs, outputs=outputs)
interface.launch()
运行这段代码后,将会在浏览器中打开一个页面,可以输入页码来查看对应的数据。默认显示第一页的结果,可以在输入框中修改页码,然后点击"Submit"按钮获取对应页码的数据。数据以二维表格的形式显示在页面上。