0

The question here might be repeated, but I was not able to get a solution from any material available online. I am trying to get the value of the Selected Dropdown list as a string, So that later based on the selected item I can select the type of plot to be displayed. But I'm getting a 'form' error.

HTML code of the form:

        <form action="{{ url_for('visualize') }}" method="GET">
            Select one from the given options:

            <select name="plots">
                <option value="{{Selected_plot[0]}}" selected>{{Selected_plot[0]}}</option>
                {% for plots in Selected_plot[1:] %}
                <option value="{{plots}}">{{plots}}</option>
                {% endfor %}
            </select>
            <input type="submit" value="Submit" />
        </form>

python code :

@app.route('/')
def home():
    Selected_plot = ['Scatter plot', 'Line Plot','Box Plot']
    return render_template('homepage.html' )


@app.route('/visualize', methods=['POST', 'GET'] )
def visualize():
    """Renders the contact page."""
    if request.method == "GET":
        plot_selected = str(request.form['plots'])

    if plot_selected ==  'Scatter plot':
        #Scatter plot code 
    elif plot_selected ==  'Line Plot':
        # Line plot code
    else:
        # Box plot code 

Thanks to the Community in advance.

1 Answers1

0

Try

request.values.get("plots", <default_value>)

Note: <default_value> is whatever you wish to be the value if the program can't find the variable "plots". You can skip it if you're sure you'll always send the variable, plots.

The advantage of this format (request.values.get) is that it will work with both POST & GET (i.e. forms data passed through a POST/form-body or just query string)

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15