How can I get the <select> tag option chosen by the user using Flask?
<p>I have a form whereby a user uploads a csv file, that csv file is then converted to a pandas dataframe and the column headers automatically populate the <code>&lt;select&gt;</code> tag options in the form.</p>

<p>I then want the user to select the column that they want and I want that column choice saved in a variable using Flask.</p>

<p>However, I'm having trouble retrieving the option that the user then selects from this select tag. </p>

<p>My template code:</p>

<pre><code>&lt;form class="form-horizontal" action="" method="post" enctype="multipart/form-data"&gt;
  &lt;fieldset&gt;
     &lt;legend&gt;Text Analytics&lt;/legend&gt;
     &lt;div class="form-group form-group-lg"&gt;
        &lt;label for="inputData" class="col-lg-2 control-label"&gt;Choose Data File:&lt;/label&gt;
        &lt;div class="col-lg-5"&gt;
           &lt;input type="file" class="form-control" required="required" autofocus="autofocus" name="inputData"/&gt;
        &lt;/div&gt;
        &lt;div class="col-lg-3"&gt;
            &lt;button class="btn btn-primary" type="submit" name="upload"&gt;Upload&lt;/button&gt;
        &lt;/div&gt;
        {% if success %}
        &lt;div class="alert alert-success"&gt;
            &lt;a href="#" class="close" data-dismiss="alert" aria-label="close"&gt;&amp;times;&lt;/a&gt;
            &lt;strong&gt;Success!&lt;/strong&gt; {{success}}.
        &lt;/div&gt;
        {% endif %}
        {% if error %}
        &lt;div class="alert alert-warning"&gt;
            &lt;a href="#" class="close" data-dismiss="alert" aria-label="close"&gt;&amp;times;&lt;/a&gt;
            &lt;strong&gt;Error:&lt;/strong&gt; {{error}}.
        &lt;/div&gt;
        {% endif %}
    &lt;/div&gt;
     &lt;div class="form-group form-group-lg"&gt;
        &lt;label for="colSelect" class="col-lg-2 control-label"&gt;Select column for analysis:&lt;/label&gt;
        &lt;div class="col-lg-5"&gt;
            &lt;select class="form-control" name="colSelect" id="colSelect"&gt;
            {% for column in columns %}
                &lt;option id="{{column}}"&gt;{{column}}&lt;/option&gt;
            {% endfor %}
            &lt;/select&gt;
        &lt;/div&gt;
</code></pre>

<p>My Flask code:</p>

<pre><code>@app.route('/textanalytics', methods = ['GET', 'POST'])
def upload_file():
error = None
success = None
columns = []
col = None
if request.method == "POST":
    if request.files['inputData'] == '':
        error = "No file selected"
    else:
        file = request.files['inputData']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            success = 'File uploaded'
            data = pd.read_csv(os.path.join(app.config['UPLOAD_FOLDER'], filename), header = 0, low_memory = False)
            columns = [i for i in data.columns]
            col = request.form.get('colSelect')
            return render_template('textanalytics.html', success = success, columns = columns, col = col)
        elif file and not allowed_file(file.filename):
            error = 'Incorrect file type, .csv only'
            return render_template('textanalytics.html', error = error)
return render_template('textanalytics.html', error = error, success = success, columns = columns, col = col)


app.add_url_rule('/uploads/&lt;filename&gt;', 'uploaded_file', build_only=True)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {'/uploads':  app.config['UPLOAD_FOLDER']})
</code></pre>

<p>As you can see, I am using <code>request.form.get('colSelect')</code> to retrieve the option but without any luck. It just returns <code>None</code> which is its initialised value.</p>

<p>I have a feeling it has to do with where I am placing that code but I am new to Flask and so could do with some help.</p>

<p>You have to give each <code>option</code> a <code>value</code>. </p>

<pre><code>&lt;option value="{{ column }}"&gt;{{ column }}&lt;/option&gt;
</code></pre>

javascript python html flask