当前位置: 代码网 > it编程>前端脚本>Python > python中gradio的输出展示组件实例代码

python中gradio的输出展示组件实例代码

2024年11月20日 Python 我要评论
html:展示html内容,适用于富文本或网页布局。json:以json格式展示数据,便于查看结构化数据。keyvalues:以键值对形式展示数据。label:展示文本标签,适用于简单的文本输出。ma
  • 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 "<h1>hello, gradio!</h1><p>this is an html output.</p>"

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",
    title="列表输入示例"
).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()

总结 

到此这篇关于python中gradio的输出展示组件的文章就介绍到这了,更多相关python gradio输出展示组件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com