python gradio 的输出展示组件
- HTML:展示HTML内容,适用于富文本或网页布局。
- JSON:以JSON格式展示数据,便于查看结构化数据。
- KeyValues:以键值对形式展示数据。
- Label:展示文本标签,适用于简单的文本输出。
- Markdown:支持Markdown格式的文本展示。
- Plot:展示图表,如matplotlib生成的图表。
- Text:用于显示文本,适合较长的输出。
1、json列子
import gradio as gr
import json
# 示例 JSON 数据
json_data = {
"name": "Gradio",
"type": "Library",
"languages": ["Python", "JavaScript"],
"description": "Gradio is an open-source library that allows developers to build interactive applications with machine learning and data science projects."
}
# 将 JSON 数据转换为字符串格式
json_str = json.dumps(json_data, indent=4)
# 定义一个函数,它接受没有输入,并返回 JSON 字符串
def show_json():
return json_str
# 使用 Gradio 创建界面,JSON 组件展示数据
gr.Interface(fn=show_json,inputs=None, outputs='json').launch()
没有输入,点击generate显示了json数据
2、html
import gradio as gr
def show_html():
return "Hello, Gradio!
This is an HTML output.
"
gr.Interface(
fn=show_html,
inputs=None,
outputs="html"
).launch()
3、plot
import gradio as gr
def process_list(my_list):
# 对列表进行处理的示例函数
return f"接收到列表,长度为: {my_list}"
# 创建一个包含列表输入的界面
gr.Interface(
process_list,
gr.List(label="输入列表"), # 定义输入为列表
"text",
).launch()
import gradio as gr
import plotly.graph_objects as go
# 创建一个简单的Plotly图表
def create_plot(x_data, y_data):
fig = go.Figure(data=go.Bar(x=x_data[0], y=y_data[0]))
return fig
# 创建Gradio界面
interface = gr.Interface(
fn=create_plot,
inputs=[
gr.List(label="X Axis Data"),
gr.List(label="Y Axis Data"),
],
outputs='plot',
)
# 运行Gradio界面
interface.launch()
4、markdown
import gradio as gr
# with open("example.md", "r") as f:
# md_content = f.read()
def show_markdown(markdown_text):
return markdown_text
interface = gr.Interface(
fn=show_markdown,
inputs=gr.Textbox(lines=10), # value = md_content
outputs=gr.Markdown()
)
interface.launch()
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!





