0

I am not sure if I am doing this correctly. I have a list of csv files generated by dump from another script in a directly. I am trying to list files, then if name is clicked, allow downloading the file, if a report link next to the name is clicked, render in html table.

Here is what I came up so far.

from flask import Flask, render_template, send_file
import tablib
import os

app = Flask (__name__)
@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
    BASE_DIR = '/data/reports/'
    abs_path = os.path.join(BASE_DIR, req_path)
    # If does not exit
    if not os.path.exists(abs_path):
        return "Not Found!"
    # If file exists
    if os.path.isfile(abs_path):
        return send_file(abs_path)
    # List files
    files = os.listdir(abs_path)
    return render_template('files.html', files=files)

# Render data in html
@app.route('/report/', defaults={'req_file': ''})
@app.route('/<req_file>')
def index(req_file):
    dataset = tablib.Dataset()
    with open(req_file) as f:
        dataset.csv = f.read()
    data = dataset.html
    return render_template('index.html', data=data)

In files.html, I have the following template.

<!DOCTYPE html>
<html>
<head>
 <title>Available Reports</title>
</head>
<body>
<ul>
    {% for file in files %}
    <li><a href="{{ file }}">{{ file }}</a> - <a href="/report/">Report</a></li>
    {% endfor %}
</ul>
</body>
</html>

In index.html, the following template.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/reportstyle.css') }}"/>
  <title>Report</title>
</head>
<body>
 <div class="tftable">
    {% block body %}
    {{ data|safe }}
    {% endblock %}  
</div>
</body>
</html>

Testing each separately, work as expected, but I can't get flask to render the csv file when clicked from the list.

R J
  • 1,906
  • 1
  • 17
  • 15
  • 1
    You need to make those file downloadable, by providing the absolute path and using `send_from_directory` while passing the files to the frontend. Please follow this thread https://stackoverflow.com/questions/24577349/flask-download-a-file, this will help you. – user2906838 Aug 02 '18 at 05:20
  • The download works, but I guess I did not explain properly, what I am trying to do. The objective is to have the user click the report link next to the file name in the listing which would allow rendering the file to html. I'll go through the suggestions in the thread. I appreciate the pointer. Thanks. – R J Aug 02 '18 at 07:10
  • Oh Ok, so you're unable to render the files content in the html table? – user2906838 Aug 02 '18 at 07:20
  • Yep, clicking the Report link in the files.html, produces 404, which I can't figure out how to parse the file with its full path and send it to the 2nd route, `/report` in the app. – R J Aug 02 '18 at 07:29
  • Ok, you can actually send the fileid as a param in the `/report` route, and then parse it and send it as a dict or something depending upon your data. – user2906838 Aug 02 '18 at 07:32
  • 1
    Thanks. I'll experiment with that. I am still a bit new to Flask. I appreciate the pointers. – R J Aug 02 '18 at 07:38

0 Answers0