python中g(shù)radio的輸出展示組件實例代碼
更新時間:2024年11月19日 09:19:22 作者:大霞上仙
這篇文章主要介紹了python中g(shù)radio的輸出展示組件的相關(guān)資料,文章介紹了多種數(shù)據(jù)展示格式,包括HTML、JSON、KeyValues、Label、Markdown和Plot,每個格式都有其適用場景,需要的朋友可以參考下
- HTML:展示HTML內(nèi)容,適用于富文本或網(wǎng)頁布局。
- JSON:以JSON格式展示數(shù)據(jù),便于查看結(jié)構(gòu)化數(shù)據(jù)。
- KeyValues:以鍵值對形式展示數(shù)據(jù)。
- Label:展示文本標簽,適用于簡單的文本輸出。
- Markdown:支持Markdown格式的文本展示。
- Plot:展示圖表,如matplotlib生成的圖表。
- Text:用于顯示文本,適合較長的輸出。
1、json列子
import gradio as gr
import json
# 示例 JSON 數(shù)據(jù)
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 數(shù)據(jù)轉(zhuǎn)換為字符串格式
json_str = json.dumps(json_data, indent=4)
# 定義一個函數(shù),它接受沒有輸入,并返回 JSON 字符串
def show_json():
return json_str
# 使用 Gradio 創(chuàng)建界面,JSON 組件展示數(shù)據(jù)
gr.Interface(fn=show_json,inputs=None, outputs='json').launch()沒有輸入,點擊generate顯示了json數(shù)據(jù)

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):
# 對列表進行處理的示例函數(shù)
return f"接收到列表,長度為: {my_list}"
# 創(chuàng)建一個包含列表輸入的界面
gr.Interface(
process_list,
gr.List(label="輸入列表"), # 定義輸入為列表
"text",
title="列表輸入示例"
).launch()
import gradio as gr
import plotly.graph_objects as go
# 創(chuàng)建一個簡單的Plotly圖表
def create_plot(x_data, y_data):
fig = go.Figure(data=go.Bar(x=x_data[0], y=y_data[0]))
return fig
# 創(chuàng)建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()
總結(jié)
到此這篇關(guān)于python中g(shù)radio的輸出展示組件的文章就介紹到這了,更多相關(guān)python gradio輸出展示組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3 QT5 端口轉(zhuǎn)發(fā)工具兩種場景分析
這篇文章主要介紹了python3 QT5 端口轉(zhuǎn)發(fā)工具,功能是打開本機端口,映射到指定IP的端口,接下來通過兩種場景給大家詳細介紹,感興趣的朋友一起看看吧2022-01-01
python實現(xiàn)刪除列表中空字符串元素的兩種方法
本文主要介紹了python實現(xiàn)刪除列表中空字符串元素的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03

