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()

相关推荐

  1. 洛谷p1157组合输出

    2024-07-19 22:52:04       41 阅读
  2. Vue3使用component动态展示组件

    2024-07-19 22:52:04       31 阅读
  3. 输入输出系统组成以及i/o设备与主机联系

    2024-07-19 22:52:04       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 22:52:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 22:52:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 22:52:04       45 阅读
  4. Python语言-面向对象

    2024-07-19 22:52:04       55 阅读

热门阅读

  1. 讲一讲你理解的虚拟内存

    2024-07-19 22:52:04       21 阅读
  2. Android init.rc如何并行执行任务

    2024-07-19 22:52:04       22 阅读
  3. 如何用BeautifulSoup批量下载美女图片

    2024-07-19 22:52:04       17 阅读
  4. 华为OD机考题(基础API)

    2024-07-19 22:52:04       17 阅读
  5. 几种典型的锁

    2024-07-19 22:52:04       15 阅读
  6. 细水长流:使用Scikit-Learn实现模型的增量预测

    2024-07-19 22:52:04       20 阅读
  7. 云计算复习--云计算机制

    2024-07-19 22:52:04       19 阅读