0

I have a flask app which asks a user to upload a csv file. It then does some calculations and returns a table on a web page. I would like the user to then be able to download the table by pressing a button. I am having great trouble trying to understand how to download the outputted table (either the panda or the html table):

Here is my code:

from flask import Flask, make_response, request, render_template
import pandas as pd
from werkzeug.utils import secure_filename
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
from flask import send_file
from dominate.tags import img
import requests
import json
import numpy as np
import pytest

app = Flask(__name__)
Bootstrap(app)


@app.route('/')
def form():
    return render_template('index.html')


@app.route('/barrPath', methods=["POST"])
def barrPath_view():
    if request.method == 'POST':
        # data_file is the name of the file upload field
        f = request.files['data_file']

        # for security - stops a hacker e.g. trying to overwrite system files
        filename = secure_filename(f.filename)

        # save a copy of the uploaded file
        f.save(filename)

        # Do some processing
        df = barrdf(filename)
        df_predictions = barr_Process(df)
        df_predictions = barrAlgo(df_predictions, filename)
        # dl_and_table(df_predictions, filename)

        return render_template('table_viewer.html', tables=[df_predictions[1].to_html(classes='table table-striped')],
                               titles=df_predictions[1].columns.values)


def barrdf(filename):

    df = pd.read_csv(filename)

    df.style.set_table_styles([{'selector': '',
                                'props': [('border',
                                           '10px solid yellow')]}])
    return df


def barr_Process(df):
    # Do a load of predictions
    df_predictions=df
    
    return df_predictions

def barrAlgo(df_predictions, filename):
#Do a whole load more processing

df_predictions =df_predictions 
    return df_predictions


#@app.route('/barrPathPredict', methods=["POST"])
#    def PathPredict(df_predictions):
#       return send_file(df_predictions,
#                        mimetype='"text/csv"',
#                        as_attachment=True,
#                        attachment_filename="My_predictions"
#                        )

if __name__ == '__main__':
    app.run("0.0.0.0", port=8080, debug=True)

Here is my html:

<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <title>Table view</title>
</head>
<style>
    table.dataframe, .dataframe th, .dataframe td {
  border: none;
  border-bottom: 1px solid #C8C8C8;
  border-collapse: collapse;
  text-align:left;
  padding: 10px;
  margin-bottom: 40px;
  font-size: 0.9em;
  max-width: 150px;
}


</style>
<body>
{% extends "layout.html" %}
            {% block content %}
<div class="inner">
<form action="/barrPathPredict" method="post" enctype="multipart/form-data">
                    <input type="submit" value="Download"/>
                </form>
</div>

{% for table in tables %}
            {{titles[loop.index]}}
            {{ table|safe }}
{% endfor %}

{% endblock %}

</body>
</html>

As you can see in the @app.route('/barrPathPredict', I have tried to return the table but I think the original panda is out of scope. How can I download the returned html table or the df_predictions from the @app.route('/barrPath'

Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125

1 Answers1

0

I see few options here:

Radoslav Bodó
  • 613
  • 5
  • 19