那里有烧瓶专家吗?

问题描述 投票:0回答:0

我已经制作了一个药理学研究管道,我想将其部署在一个网站上。我做了一个简单的网站,我在其中接受用户输入,如姓名、疾病列表、项目名称和输入文件。

后端开发,我用的是flask。我的管道需要几分钟的时间来生成输出。

问题是我无法将我的管道代码与 Flask 应用程序集成。

这是我的代码:

from flask import Flask, request, render_template, redirect, send_file, make_response, url_for
from werkzeug.utils import secure_filename
import os
import uuid
from zipfile import ZipFile
from mycode import process

app = Flask(__name__)
app.config['UPLOAD_DIRECTORY'] = 'uploads/'
app.config['ALLOWED_EXTENSIONS'] = ['.sdf']


@app.route('/')
def home():
    return render_template('flask.html')


# Define a dictionary to store user tokens
user_tokens = {}


# Define a function to authenticate a user's token
def authenticate_token(token):
    return token in user_tokens.values()


@app.route('/upload', methods=['POST', 'GET'])
def upload():
    username = request.form['username']
    project_name = request.form['project_name']
    user_id = str(uuid.uuid4())

    # Create a directory with the user id as the directory name
    user_folder_path = os.path.join(app.config['UPLOAD_DIRECTORY'], user_id)
    os.makedirs(user_folder_path, exist_ok=True)
    file = request.files['sdf_file']
    extension = os.path.splitext(file.filename)[1].lower()
    new_filename = f"input_{extension}"
    if file:
        if extension not in app.config['ALLOWED_EXTENSIONS']:
            return "the file is not an SDF."
        file.save(
            os.path.join(
                user_folder_path,
                new_filename
            )
        )
    selected_options = request.form.getlist('options[]')
    # Store the token in the user_tokens dictionary
    user_tokens[username] = user_id
    # Return a response with the token as a cookie
    response = make_response("File uploaded successfully!")
    response.set_cookie('token', user_id)
    response.set_cookie('user_id', user_id)




    #result = process(selected_options, new_filename, user_id)
    return redirect(f'/download_file?user_id={user_id}&token={user_id}')

# Define a Flask route for handling user output file downloads
@app.route('/download_file',methods=['GET','POST'])

def download_file():
    # Retrieve user_id and new_filename from URL
    user_id = request.args.get('user_id')
    # Authenticate the user's token
    token = request.args.get('token')
    if not authenticate_token(token):
        return "Unauthorized access!", 401

    # Check that the user has permission to download the specified file

    # Generate a download link for the file
    if token == user_id:
        user_folder_path = os.path.join(app.config['UPLOAD_DIRECTORY'], user_id)
        files = [
            os.path.join(user_folder_path, "input_.sdf")
        ]
        # Create a zip file containing all the selected files
        zip_path = os.path.join(app.config['UPLOAD_DIRECTORY'], f"{user_id}.zip")
        with ZipFile(zip_path, 'w') as zip_file:
            for file_name in files:
                zip_file.write(file_name)

        # Offer the zip file for download
        return send_file(zip_path, as_attachment=True)
    else:
        return "You do not have permission to download this file", 403

if __name__ == '__main__':
    app.run(debug=True) 

我尝试导入一个以文件形式生成输出的函数。但我猜我的应用程序无法运行它。当我在 Flask 应用程序外运行时,管道会完美地生成输出,但当我将它添加到我的 Flask 应用程序时,它不会生成输出。

python flask web-deployment-project
© www.soinside.com 2019 - 2024. All rights reserved.