Flask将CSV文件内容分配给内存并在HTML页面上显示内容

时间:2018-05-10 07:47:25

标签: python flask

我正在尝试构建一个烧瓶应用程序,它将加载一个csv文件,将其分配给内存,然后在HTML页面上显示内容,

我正在使用的代码,如下所示,当我运行代码并尝试加载文件时,我收到以下错误,

属性错误:' _io.BytesIO'对象没有属性'文件

烧瓶

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    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))

            ## snippet to read code below
            file.stream.seek(0) # seek to the beginning of file
            myfile = file.file # will point to tempfile itself
            dataframe = pd.read_csv(myfile)
            ## end snippet

            return "yatta"
        else:
            return "file not allowed"

    return render_template("upload.html")

HTML

<div class="form-group">
    <form method="POST" enctype=multipart/form-data>
    <input class="btn btn-sm btn-primary" type="file" name="file">
    <input type="submit" value='upload'>
</div>
<table border="1" cellpadding="5" cellspacing="5">
            {% for row in data %}
                <tr>
                {% for d in row %}
                    <td>{{ d }}</td>
                {% endfor %}
            </tr>
            {% endfor %}
        </table>

0 个答案:

没有答案