cellpose-web/backend/flaskApp.py
ClovertaTheTrilobita 92a89f856a feature(frontend): 前端新增跳转至预览
TODO: 获取的文件发送给cellpose,将cellpose的运行状态、预览发送给前端
2025-09-16 22:41:05 +03:00

46 lines
No EOL
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, send_from_directory, request, jsonify
import os, shutil, time, threading, datetime
from werkzeug.utils import secure_filename
from flask_cors import CORS
from pathlib import Path
app = Flask(__name__)
CORS(app)
BASE_DIR = Path(__file__).resolve().parent
UPLOAD_DIR = BASE_DIR / "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
def run_flask():
app.run(host="10.147.18.141", port=5000)
@app.route("/")
def index():
return "<h1>Hello</h1><p>This is the backend of our cellpose server, please visit our website.</p>"
@app.route("/testdl")
def test_download():
return send_from_directory("test_output/2025-09-16-20-03-51", "img_overlay.png", as_attachment=True)
@app.route("/dl/<timestamp>")
def download(timestamp):
input_dir = os.path.join("output", timestamp)
output_dir = os.path.join("output/tmp", timestamp) # 不要加 .zipmake_archive 会自动加
os.makedirs("output/tmp", exist_ok=True) # 确保 tmp 存在
shutil.make_archive(output_dir, 'zip', input_dir)
print(f"压缩完成: {output_dir}.zip")
return send_from_directory("output/tmp", f"{timestamp}.zip", as_attachment=True)
@app.post("/upload")
def upload():
ts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
os.makedirs(UPLOAD_DIR / ts, exist_ok=True)
files = request.files.getlist("files")
saved = []
for f in files:
if not f or f.filename == "":
continue
name = secure_filename(f.filename)
f.save(os.path.join(UPLOAD_DIR / ts, name))
saved.append(name)
return jsonify({"ok": True, "count": len(saved), "files": saved, "id": ts})