Taking Data from Drop-Down Menu Using Flask

Taking data from drop-down menu using flask

You have just to tell the flask method to accept POST request and to read parameters from the request

Example:

from flask import Flask, request
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
myvariable = request.form.get("teamDropdown")
... your code ...
return "hello world"

How to grab the value from the drop-down menu using flask

below code works on me,

from flask import Flask, render_template, request, redirect
app = Flask(__name__)
app.debug = True


@app.route('/', methods=['GET'])
def dropdown():
colours = ['SBI', 'Kotak', 'Citi', 'AMEX', 'BOB', 'AXIS', 'HDFC', 'IDBI', 'YES', 'IndusInd']
return render_template('feba.html', colours=colours)


@app.route('/dropdown', methods = ['POST'])
def dropp():
dropdownval = request.form.get('colour')
print(dropdownval)
return redirect("/", code=302)
if __name__ == "__main__":
app.run()

<!DOCTYPE html>
<html lang="en">

<style>
body {text-align:center}
</style>

<head>
<meta charset="UTF-8">
</head>

<body>
<img src="{{url_for('static', filename='logo_1.jpg')}}" align="middle" />
<h1>KARDFINDER</h1>
<h2>json file generator for banks</h2>
<form method="POST" action="/dropdown">
<select name="colour">
<option value="{{colours[0]}}" selected>{{colours[0]}}</option>
{% for colour in colours[1:] %}
<option value="{{colour}}">{{colour}}</option>
{% endfor %}
</select>
<input type="submit" value ="Submit">
</form>
</body>
</html>

you should learn http requests, html forms and ajax

Get the dropdown selected value in bootstrap python flask

Bartosz's answer is valid, but the below code does the same without using Javascript

html

<form method="POST" action="{{ url_for('test') }}">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle text-right " type="button" id="dropdownMenu3" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
age
</button>
<div class="dropdown-menu pre-scrollable" aria-labelledby="dropdownMenu3">
{% for i in range(110) %}
<button name = "age" value = {{i}} class="dropdown-item text-right " type="submit">{{i}}</button>
{% endfor %}
</div>
</div>
</form>

app.py

@app.route('/test', methods=['POST', 'GET'])
def test():
if request.method == 'POST':
print(request.form.get('age'))
return render_template("test.html")

you need wrap the dropdown div in Form and specify its action.

Dropdown menu from Python list using Flask and HTML

<form action="{{ url_for('upload') }}" method="post">

Your current form tag's action attribute isn't defined so it's not sending data anywhere when you submit it.

Unable to get value from flask dropdown menu

I am skipping any code related to matplotlib and graphing package.

Rather, I am showing an example of handling the dropdown value in Flask.
The following example will show value based on the month selected by user.

app.py:

from flask import Flask, render_template, url_for, request, redirect 


application = Flask(__name__)

def get_monthly_data(month):
data = {
"January": "First month of the year",
"February": "Second month of the year",
"March": "Third month of the year",
"April": "Fourth month of the year",
"May": "Fifth month of the year"
}
return data.get(month, "Data is not found").strip()

@application.route('/', methods=['GET', 'POST'])
def home():
if request.method == "POST":
month = request.form.get('month')
return render_template('home.html', data = get_monthly_data(month))
return render_template('home.html')

if __name__ == '__main__':
application.run(debug = True)

home.html:

<html>
<head>
<title>Dropdown Example</title>
</head>
<body>
{% if data %}
<div>
<h3>Monthly Data</h3>
<p>{{ data }}</p>
</div>
{% endif %}
<form action="/" method="post">
<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March" selected >March</option>
<option value="April">April</option>
<option value="May">May</option>
</select>
<input type="submit" value="Select Month">
</form>
</body>
</html>

Output:

Form with month dropdown:

form with month selection dropdown

Showing result based on user selection:

showing result based on user selection

Get data from drop menu rather than text input using flask

Form elements require names— your example of a select field:

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

Needs to be more like:

<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

Now your view can call the chosen result by calling request.form["cars"]



Related Topics



Leave a reply



Submit