python-ui框架
目录
python ui框架
gradio python模块做交互界面很好用
ui拖拽
import gradio as gr
import os
from datetime import datetime
def process_uploaded_file(files):
"""处理上传的文件并返回文件信息"""
file_info = []
for file in files:
if file is None:
continue
# 获取文件信息
file_name = os.path.bas(file.name)
file_size = os.path.getsize(file.name)
file_extension = os.path.splitext(file.name)[1].lower()
upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 转换为更友好的文件大小格式
size_units = ['B', 'KB', 'MB', 'GB']
size_index = 0
while file_size >= 1024 and size_index < len(size_units) - 1:
file_size /= 1024.0
size_index += 1
file_info.append(
{"文件名": file_name, "大小": f"{file_size:.2f} {size_units[size_index]}", "类型": file_extension,
"上传时间": upload_time})
# 创建信息表格
if not file_info:
return "没有文件上传"
info_html = "<table><tr><th>文件名</th><th>大小</th><th>类型</th><th>上传时间</th></tr>"
for info in file_info:
info_html += f"<tr><td>{info['文件名']}</td><td>{info['大小']}</td><td>{info['类型']}</td><td>{info['上传时间']}</td></tr>"
info_html += "</table>"
return info_html
# 创建 Gradio 界面
with gr.Blocks(title="文件上传示例") as demo:
gr.Markdown("# 🗂️ 文件上传示例")
gr.Markdown("拖拽文件到下方区域或点击选择文件进行上传")
with gr.Row():
file_input = gr.File(label="上传文件", file_count="multiple", # 允许多个文件
file_types=[ # 允许的文件类型
".txt", ".pdf", ".png", ".jpg", ".jpeg", ".gif", ".mp3", ".mp4", ".doc", ".docx", ".xls", ".xlsx"])
with gr.Row():
output = gr.HTML(label="文件信息")
# 设置文件上传时的处理函数
file_input.upload(fn=process_uploaded_file, inputs=file_input, outputs=output)
# 启动应用
if __name__ == "__main__":
demo.launch()