I am currently trying to figure out a table that is generated via query which uses reactive user input values, and I cannot for the life of me figure out how to make this happen. I've found similar questions, but no solutions have worked for me. I need help!
Here are the questions I've reviewed but can't seem to make anything work:
- Flask button to save table from query as csv
- Return a response with an download option on the same HTML page using flask'
- Create and download a CSV file from a Flask view
(This is all just example code as I can't post my actual code)
So, the route for the page showing the data is:
routes.py
from flask import render_template, flash, redirect, url_for, request, jsonify, send_file, make_response
from app import app
from app.forms import MyDataForm, MyDataTable
from app.models import MyData
@app.route('/my_data', methods=['GET', 'POST'])
def my_data():
   myData = MyData.query.all()
   form = MyDataForm()
   table = MyData(myData)
   if request.method == 'GET':
      return render_template('my_data.html', title = 'My Data Table', form = form, myData = myData)
   elif request.method == 'POST':  
      selected = request.form.get('my_data_type')
      print(selected)
      table.col1.show = table.col2.show = table.col3.show = False
      if int(selected) == 1:
         myData = MyData.query.with_entities(MyData.col1, MyData.col2, MyData.col3)
         table = MyTable(myData)
         table.col1.show = True
      elif int(selected) == 2:
         myData = MyData.query.with_entities(MyData.col1, MyData.col2)
         table = MyTable(myData)
         table.col1.show = True
      else:
         myData = MyData.query.with_entities(MyData.col2, MyData.col3)
         table = MyTable(myData)
         table.col2.show = True
   if form.validate_on_submit() and request.form['form_name'] == 'Metric':
   return redirect(url_for('my_data'))
My forms file:
forms.py
from flask_table import Table, Col
class MyDataForm(FlaskForm):
   my_data_type = SelectField('Metric', choices = [(1, "Option 1"), (2, "Option 2"), (3, "Option 3")], default = 1)
   submit = SubmitField('Select Metric')
class MyTable(Table):
   classes = ['table', 'table-condensed']
   col1 = Col('Col1', show = False)
   col2 = Col('Col2', show = False)
   col3 = Col('Col3', show = False)
And then my html template for the page with the table:
my_data.html
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<div class = "container">
   <form action = "" method = "post">
      <fieldset>
         <legend>Select a metric:</legend>
         <div class ="form-group required">
            {{ form.my_data_type.label(class="form-control-label") }}
            {{ form.my_data_type(class="form-control") }}
         </div>
         <div class = "form-group">
            {{ form.submit(class="form-control") }}
         </div>
      </fieldset>
   </form>
   <!-- VVVV How can I link the returned data query to this download link?! VVV-->
   <a href="/return_table/" target="blank"><button class='btn btn-default'>Download Table!</button></a>
   <h3>My Data:</h3>
      {{ table }}
</div>
{% endblock %}
