Good Evening.
Hope you doing great.
I need to get correct result when I pass <value>
in route..
Later I will be connecting 2 dropdowns (1st - countries and 2nd - files for specific country) in my flask app, but before that I need to accomplish this task.
app.py:
from flask_wtf import FlaskForm
from wtforms import SelectField
from flask import Flask, render_template, request, jsonify
import os
# create Flask
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
germany_cars = ["audi.csv", "bmw.csv", "mercedes.csv"]
japanese_cars = ["lexus.csv", "infiniti.csv", "toyota.csv"]
italian_cars = ["ferrari.csv", "lamborghini.csv", "maserati.csv"]
class Form(FlaskForm):
country = SelectField('car', choices = [("Germany"), ("Japan"), ("Italy")])
file = SelectField("file", choices = [])
@app.route("/", methods = ['GET', 'POST'])
def index():
form = Form()
form.country.choices = [(i) for i in form.country.choices]
form.file.choices = []
return render_template("index.html", form=form)
@app.route("/cars/<value>")
def cars(value):
countries = ["Germany", "Japan", "Italy"]
carsArray = []
for i in countries :
carsObj = {}
carsObj["country"] = i
if i == "Germany":
carsObj["file"] = germany_cars
elif i == "Japan":
carsObj["file"] = japanese_cars
else:
carsObj["file"] = italian_cars
carsArray.append(carsObj)
if value in countries:
return jsonify({"Countries": carsArray})
break
else:
return jsonify("")
break
if __name__ == "__main__":
app.run(debug = True)
index.html:
<form method = "POST">
<div class="col-md-auto mb-3">
{{form.csrf_token}}
{{form.country}}
{{form.file}}
<input class="btn btn-primary" type = "submit" value = "Load File">
</div>
</form>
image:
As you can see, I pass Italy and see information for Germany.
Hope you can help.
Thank you :)