Step 4: Integration
This last but most important step in this project. Till now we have not seen any visualization on our app.
Now we will import all the models from models.py into app.py like this:
from flask import Flask, render_template, request, session, redirect, url_for, flash
from models import plot_daywise_bar, ratio_dayWise, \
plot_daywise_line, case_over_time_map, top_twenty_hbar, \
plot_hbar_wm, plot_stacked, plot_line, scatter
Now we will define the paths of the resources which we have stored in assets folder into variables as string.
country_wise_latest = 'covidtrack/assets/country_wise_latest.csv'
covid_19_clean_complete = 'covidtrack/assets/covid_19_clean_complete.csv'
day_wise = 'covidtrack/assets/day_wise.csv'
full_grouped = 'covidtrack/assets/full_grouped.csv'
usa_county_wise = 'covidtrack/assets/usa_county_wise.csv'
worldometer_data = 'covidtrack/assets/worldometer_data.csv'
Now we will modify the root route to call data from models.py and we will store this data into variable. And at last we will send these data to index.html so that it could display it.
@app.route('/')
@app.route('/index.html')
def root():
bar = plot_daywise_bar(day_wise)
line = plot_daywise_line(day_wise)
topTwentyCases = top_twenty_hbar(country_wise_latest, 'Confirmed', 15)
topTwentyDeaths = top_twenty_hbar(country_wise_latest, 'Deaths', 15)
topTwentyRecovered = top_twenty_hbar(country_wise_latest, 'Recovered', 15)
topTwentyActive = top_twenty_hbar(country_wise_latest, 'Active', 15)
topTwentyNewCases = top_twenty_hbar(country_wise_latest, 'New cases', 15)
topTwentyNewDeaths = top_twenty_hbar(country_wise_latest, 'New deaths', 15)
topTwentyDeaths_100_cases = top_twenty_hbar(country_wise_latest, 'Deaths / 100 Cases', 15)
topTwentyNewRecovered = top_twenty_hbar(country_wise_latest, 'New recovered', 15)
topTwentyRecovered_100_cases = top_twenty_hbar(country_wise_latest, 'Recovered / 100 Cases', 15)
topTwenty1_week_change = top_twenty_hbar(country_wise_latest, '1 week change', 15)
topTwenty1_week_increase = top_twenty_hbar(country_wise_latest, '1 week % increase', 15)
perMDeaths = plot_hbar_wm(worldometer_data, 'Deaths/1M pop', 15)
totalTests = plot_hbar_wm(worldometer_data, 'TotalTests', 15)
perMTests = plot_hbar_wm(worldometer_data, 'Tests/1M pop', 15)
return render_template('index.html', plot=bar, line=line, topTwentyCases=topTwentyCases, topTwentyDeaths=topTwentyDeaths,
topTwentyRecovered=topTwentyRecovered, topTwentyActive=topTwentyActive, topTwentyNewCases=topTwentyNewCases,
topTwentyNewDeaths=topTwentyNewDeaths, topTwentyDeaths_100_cases=topTwentyDeaths_100_cases,
topTwentyNewRecovered=topTwentyNewRecovered, topTwentyRecovered_100_cases=topTwentyRecovered_100_cases,
topTwenty1_week_change=topTwenty1_week_change, topTwenty1_week_increase=topTwenty1_week_increase,
perMDeaths=perMDeaths, perMTests=perMTests, totalTests=totalTests
)
Likewise we will change the worldMap function.
@app.route('/worldometer')
def worldMap():
casesMap = case_over_time_map(country_wise_latest, 'Confirmed', 'Confirmed Cases')
deathsMap = case_over_time_map(country_wise_latest, 'Deaths', 'Deaths')
deaths_100_Map = case_over_time_map(country_wise_latest, 'Deaths / 100 Cases', 'Deaths / 100 Cases')
return render_template('worldMap.html',
casesMap=casesMap, deathsMap=deathsMap, deaths_100_Map=deaths_100_Map)
To display the data we need to use some JavaScript. We will use Plotly.js for performing this thing. We will use the cloudflare cdn for linking ploly.js
src="https://cdn.plot.ly/plotly-latest.min.js"
We do not need to change anything for it, we already have included it into base.html, so it will work in all of our templets.
Now we will use following lines of code to display any graph:
<div class='chart' id='variable-name'>
<script>
var graphs = {{ 'variable-name' |safe }};
var layout = {width: 900, height: 400, margin: {t: 50, b: 50, l:100, r:20}, coloraxis: {showscale: false, colorscale: "Viridis"}};
Plotly.plot('variable-name', graphs, layout);
</script>
</div>
For making different graphs we will change the variable-name and the graph will be displayed on the browser. For if we want to plot topTwentyCases we will make a <div> like this:
<div class="chart" id="topTwentyCases">
<script>
var graphs = {{ topTwentyCases|safe }};
var layout = {width: 900, height: 400, margin: {t: 50, b: 50, l:100, r:20}, coloraxis: {showscale: false, colorscale: "Viridis"}};
Plotly.plot('topTwentyCases', graphs, layout);
</script>
</div>
Now we will make changes in whole index.html like wise. I can not include this code because of it size. Hence you can find this code at https://github.com/Daniyalatjit/CoronaWebApp/blob/master/templates/index.html
After making these changes now we will show our map on the worldMap.html. After making changes in worldMap.html, it will look like as shown in next answer.
Summer Training-Internship program-2021