-1

I am trying to create a dropdown list in HTML such that when I submit a selection, the option will be passed into a Python code and re-render the HTML page.

I've managed to create a dropdown list but the submission is not being sent back properly to the Python code and I'm not sure why. Could someone please help me?

Here is my Python code so far:

from flask import Flask, render_template, request
import pandas as pd

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])

def table():

def tabledata(columns):
    table = pd.read_excel('/Users/Time Series_2021-04-03.xlsx', header=2)
    data = table[columns]
    return data

if request.method == "POST":
    col = request.form.get('column')
    columns = col
else:
    columns = ['Asset A']

columnslist = ['Asset A', 'Asset B', 'Asset C']
data = tabledata(columns)

return render_template("table.html", data = data.to_dict(orient='records'), column=columnslist)

HTML code:

       <form action="" method="POST">
            <select  action="/" method="POST">
                <option value="{{column[0]}}" selected>{{column[0]}}</option>
                {% for col in column[1:] %}
                <option value="{{column}}">{{col}}</option>
                {% endfor %}
            </select>

        <input name="column" type="submit">Submit</input>
        </form>

    <table class="table">
    <thead>
        <tr>
            <th scope="col">Column name 1</th>
        </tr>
    </thead>
    {% for row in data %}
        <tr>
            <td>{{row}}</td>
        </tr>
    {% endfor %}
    </table>
leyjl2
  • 29
  • 2

1 Answers1

1

Changing <option value="{{column}}">{{col}}</option> to <option value="{{col}}">{{col}}</option> will work.

coldy
  • 2,115
  • 2
  • 17
  • 28