0

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:

enter image description here

As you can see, I pass Italy and see information for Germany.

Hope you can help.

Thank you :)

Yara1994
  • 317
  • 2
  • 11

1 Answers1

0

You have the list of files with this naming convention. CountryName_cars. And this country name you are passing as a value in /car/ route. So I changed the function such a way that, first it will add _cars to the value. Then it will check whether we have that company cars. If yes, it will return the car list.

For example, if you are passing Germany, the function will create a string 'germany_cars'. Then it will check whether we have this list or not. We have this list, so it will take the value from the list.

germany_cars = ["audi.csv", "bmw.csv", "mercedes.csv"]
japanese_cars = ["lexus.csv", "infiniti.csv", "toyota.csv"]
italian_cars = ["ferrari.csv", "lamborghini.csv", "maserati.csv"]


@app.route("/cars/<value>")
def cars(value):
    country_file = value.lower() + '_cars'
    result = {}
    if country_file in globals():
        result['country'] = value
        result['file'] = eval(country_file)
    else:
        result['error'] = 'Country not found'
    return jsonify(result)
NavaneethaKrishnan
  • 1,213
  • 1
  • 9
  • 22