python中gradio的輸出展示組件實例代碼
更新時間:2024年11月19日 09:19:22 作者:大霞上仙
這篇文章主要介紹了python中gradio的輸出展示組件的相關資料,文章介紹了多種數據展示格式,包括HTML、JSON、KeyValues、Label、Markdown和Plot,每個格式都有其適用場景,需要的朋友可以參考下
- 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 創(chuàng)建界面,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}" # 創(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()
總結
到此這篇關于python中gradio的輸出展示組件的文章就介紹到這了,更多相關python gradio輸出展示組件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python并發(fā)編程隊列與多線程最快發(fā)送http請求方式
假如有一個文件,里面有10萬個url,需要對每個url發(fā)送http請求,并打印請求結果的狀態(tài)碼,如何編寫代碼盡可能快的完成這些任務呢2021-09-09Python統(tǒng)計列表中每個元素出現次數的4種實現
本文主要介紹了Python統(tǒng)計列表中每個元素出現次數的4種實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程
這篇文章主要介紹了哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05