python gradio 的输出展示组件

  1. HTML:展示HTML内容,适用于富文本或网页布局。
  2. JSON:以JSON格式展示数据,便于查看结构化数据。
  3. KeyValues:以键值对形式展示数据。
  4. Label:展示文本标签,适用于简单的文本输出。
  5. Markdown:支持Markdown格式的文本展示。
  6. Plot:展示图表,如matplotlib生成的图表。
  7. 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()

### Gradio 输出组件使用说明 #### 文本输出组件 Gradio 提供了多种方式来处理不同类型的输出。对于简单的文本输出,可以利用 `gr.Textbox` 组件。此组件适用于返回字符串形式的结果给前端用户。 ```python import gradio as gr def greet(name): return f'Hello {name}' iface = gr.Interface(fn=greet, inputs='text', outputs=gr.Textbox()) iface.launch() ``` #### 图像输出组件 当涉及到图像文件作为输出时,则可采用 `gr.Image` 来实现这一功能。这使得应用程序能够接收来自函数调用后的图片路径或是直接传入 PIL Image 对象并将其呈现于界面上[^1]。 ```python from PIL import Image import gradio as gr def get_image(): img_path = "path_to_your_image.jpg" return Image.open(img_path) demo = gr.Interface(fn=get_image, inputs=None, outputs="image") demo.launch() ``` #### HTML 输出组件 除了基本的数据类型外,还支持更复杂的结构化内容比如HTML片段通过设置outputs参数为 `"html"` 实现自定义样式化的消息传递[^2]。 ```python import gradio as gr def show_html(): html_content = "<h1>Hello, Gradio!</h1><p>This is an HTML output.</p>" return html_content interface = gr.Interface(fn=show_html, inputs=None, outputs="html").launch() ``` #### 视频输出组件 针对多媒体应用场合下的需求,特别是视频流的播放与录制操作,借助内置的 `gr.Video` 可轻松完成相应任务配置。该模块不仅限于本地文件读取还能连接网络资源链接进行在线预览等功能扩展。 ```python import gradio as gr def play_video(): video_url_or_path = 'sample.mp4' return video_url_or_path app = gr.Interface(fn=play_video, inputs=None, outputs=gr.Video()).launch() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值