Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ CUSTOM_CSS = """
|
|
| 7 |
/* 左侧面板 */
|
| 8 |
#left-panel {
|
| 9 |
width: 300px;
|
| 10 |
-
height: 400px; /*
|
| 11 |
overflow-y: auto; /* 超出时滚动 */
|
| 12 |
border-right: 1px solid #ccc;
|
| 13 |
padding: 10px;
|
|
@@ -58,7 +58,8 @@ def combine_action():
|
|
| 58 |
def create_zip():
|
| 59 |
"""
|
| 60 |
动态生成并返回一个 zip 文件(内存中)。
|
| 61 |
-
Gradio 会将返回的 BytesIO 作为 File
|
|
|
|
| 62 |
"""
|
| 63 |
buf = io.BytesIO()
|
| 64 |
with zipfile.ZipFile(buf, 'w') as zf:
|
|
@@ -67,20 +68,19 @@ def create_zip():
|
|
| 67 |
buf.seek(0)
|
| 68 |
return buf # 返回内存中的 zip
|
| 69 |
|
|
|
|
| 70 |
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
| 71 |
-
#
|
| 72 |
with gr.Row():
|
| 73 |
-
|
| 74 |
-
with gr.Box(elem_id="left-panel"):
|
| 75 |
gr.Markdown("**上传区域**")
|
| 76 |
uploader = gr.File(label="在此处上传文件")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
with gr.Box(elem_id="right-panel"):
|
| 80 |
gr.Markdown("**结果缩略图**")
|
| 81 |
gr.Markdown("这里可以放置处理后的缩略图或任何输出")
|
| 82 |
|
| 83 |
-
#
|
| 84 |
with gr.Row():
|
| 85 |
combine_btn = gr.Button("合并", elem_id="combine-button")
|
| 86 |
download_btn = gr.Button("下载 Zip", elem_id="download-zip")
|
|
@@ -88,8 +88,7 @@ with gr.Blocks(css=CUSTOM_CSS) as demo:
|
|
| 88 |
# 点击“合并”时(这里仅做一个演示)
|
| 89 |
combine_btn.click(fn=combine_action, inputs=[], outputs=[])
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
# 一旦函数返回文件,该组件就会显示“下载链接”
|
| 93 |
zip_file = gr.File(label="点此下载生成的 ZIP", visible=False)
|
| 94 |
|
| 95 |
# 点击“下载 Zip”时,调用 create_zip,输出到 zip_file
|
|
|
|
| 7 |
/* 左侧面板 */
|
| 8 |
#left-panel {
|
| 9 |
width: 300px;
|
| 10 |
+
height: 400px; /* 固定高度,可根据需要调节 */
|
| 11 |
overflow-y: auto; /* 超出时滚动 */
|
| 12 |
border-right: 1px solid #ccc;
|
| 13 |
padding: 10px;
|
|
|
|
| 58 |
def create_zip():
|
| 59 |
"""
|
| 60 |
动态生成并返回一个 zip 文件(内存中)。
|
| 61 |
+
Gradio 会将返回的 BytesIO 作为 File 输出,
|
| 62 |
+
用户看到下载链接后可点击保存。
|
| 63 |
"""
|
| 64 |
buf = io.BytesIO()
|
| 65 |
with zipfile.ZipFile(buf, 'w') as zf:
|
|
|
|
| 68 |
buf.seek(0)
|
| 69 |
return buf # 返回内存中的 zip
|
| 70 |
|
| 71 |
+
# 搭建Gradio界面
|
| 72 |
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
| 73 |
+
# 用 gr.Row() + gr.Column() 实现左右并排
|
| 74 |
with gr.Row():
|
| 75 |
+
with gr.Column(elem_id="left-panel"):
|
|
|
|
| 76 |
gr.Markdown("**上传区域**")
|
| 77 |
uploader = gr.File(label="在此处上传文件")
|
| 78 |
|
| 79 |
+
with gr.Column(elem_id="right-panel"):
|
|
|
|
| 80 |
gr.Markdown("**结果缩略图**")
|
| 81 |
gr.Markdown("这里可以放置处理后的缩略图或任何输出")
|
| 82 |
|
| 83 |
+
# 底部按钮(同一行)
|
| 84 |
with gr.Row():
|
| 85 |
combine_btn = gr.Button("合并", elem_id="combine-button")
|
| 86 |
download_btn = gr.Button("下载 Zip", elem_id="download-zip")
|
|
|
|
| 88 |
# 点击“合并”时(这里仅做一个演示)
|
| 89 |
combine_btn.click(fn=combine_action, inputs=[], outputs=[])
|
| 90 |
|
| 91 |
+
# 用一个隐藏的 File 组件承载生成的 zip 文件
|
|
|
|
| 92 |
zip_file = gr.File(label="点此下载生成的 ZIP", visible=False)
|
| 93 |
|
| 94 |
# 点击“下载 Zip”时,调用 create_zip,输出到 zip_file
|