title: Flask: How to get a file from renderered list send to another route to parse
body: <p>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.</p>
  
  <p>Here is what I came up so far.</p>
  
  <pre><code>from flask import Flask, render_template, send_file
  import tablib
  import os
  
  app = Flask (__name__)
  @app.route('/', defaults={'req_path': ''})
  @app.route('/&lt;path:req_path&gt;')
  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('/&lt;req_file&gt;')
  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)
  </code></pre>
  
  <p>In files.html, I have the following template.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
   &lt;title&gt;Available Reports&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;ul&gt;
      {% for file in files %}
      &lt;li&gt;&lt;a href="{{ file }}"&gt;{{ file }}&lt;/a&gt; - &lt;a href="/report/"&gt;Report&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>In index.html, the following template.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/reportstyle.css') }}"/&gt;
    &lt;title&gt;Report&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;div class="tftable"&gt;
      {% block body %}
      {{ data|safe }}
      {% endblock %}  
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Testing each separately, work as expected, but I can't get flask to render the csv file when clicked from the list.</p>