I'm looking to pass a variable to the URL when a specific navbar item is clicked. While searching through Flask's documentation, I saw a nav.Item component called nav.Item('Latest News', 'news', {'page': 1}),
and decided I'd try it myself.
If I go to URL "localhost:5000/average/recovery/" and "localhost:5000/average/mortality" it uses the same method called getAverageInfectedRate(options), and inside that method it'll either go through the "recovery" if statement or "deaths" if statement depending on options variable, passed in by the URL that's entered.
Without separating the app route into two separate methods, is it possible for me to pass the "options"variable through the navigation buttons?
nav.Bar('top', [
nav.Item('Home', 'index'),
nav.Item('# of Cases', 'getActiveCases'),
nav.Item('Mortality Rate', 'getAverageInfectedRate', {'deaths': 'deaths'}),
## clicking button would navigate to localhost:5000/average/deaths
nav.Item('Recovery Rate', 'getAverageInfectedRate', {'recovery': 'recovery'})
## clicking button would navigate to localhost:5000/average/recovery
])
This is the parameter specific If statement route, discussed above.
def getAverageInfectedRate(option):
if (option == 'deaths'):
first = requests.get("https://covid2019-api.herokuapp.com/total")
second = requests.get("https://covid2019-api.herokuapp.com/deaths")
first = first.json()
second = second.json()
convertConfirmed = int(first["confirmed"])
convertDeaths = int(second["deaths"])
stats = int(convertConfirmed/convertDeaths)
return render_template_string('''
<h1>Mortality Rate, worldwide</h2>
<h2>{{stats}}%</h2>
''', stats = stats)
if (option == 'recovery'):
first = requests.get("https://covid2019-api.herokuapp.com/total")
second = requests.get("https://covid2019-api.herokuapp.com/total")
first = first.json()
second = second.json()
convertConfirmed = int(first["confirmed"])
convertRecovered = int(second["recovered"])
stats = int(convertConfirmed/convertRecovered)
return render_template_string('''
<h1>Recovery Rate, worldwide</h2>
<h2>{{stats}}%</h2>
''', stats = stats)
I'm fairly new at web development obviously and would love for suggestions on how to make this code clearer, I thought about seperating the routes into two different ones, but I like the possibility of adding an additional parameter to select an individual country. Working with API's is so fun!