简体   繁体   English

烧瓶上传CSV文件而不保存

[英]flask upload CSV file without saving

trying to place my text classification model into flask applications using CSV file upload to read data without saving the uploaded .csv file and throw it into my classifier model print it on the result pages.尝试使用 CSV 文件上传将我的文本分类模型放入烧瓶应用程序中以读取数据而不保存上传的 .csv 文件并将其放入我的分类器模型中并将其打印在结果页面上。 below example code of my attempt :下面是我尝试的示例代码:

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            file.stream.seek(0) 
            myfile = file.file 
            dataframe = pd.read_csv(myfile)
            return
        else:
            return "Not Allowed"

    return render_template("home.html")

This is my form这是我的表格

<form action="" method=post enctype=multipart/form-data>
            <input type=file name="file[]" multiple>
            <input type=submit value=Upload>
</form> 

exception occurred here这里发生异常

NameError: name 'allowed_file' is not defined

Any idea about this kind of issue ?关于这种问题的任何想法?

I think you are using this part of documentation : ( http://flask.pocoo.org/docs/0.12/patterns/fileuploads/ )我认为您正在使用这部分文档:( http://flask.pocoo.org/docs/0.12/patterns/fileuploads/

But you have to add the function :但是你必须添加函数:

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS`

Have you created a function called allowed_file() in your module?您是否在模块中创建了一个名为allowed_file()的函数? Or have you created it in another module and forgotten to import it?或者您是否在另一个模块中创建了它而忘记导入它? You're feeding your filename into the function allowed_file() so that it can check whether the filename is permitted, but the NameError indicates that the function allowed_file() cannot be found.您将文件名输入到函数allowed_file()以便它可以检查文件名是否被允许,但NameError表示找不到函数allowed_file()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM