using for loop in jinja 2, dictionaries, filling data in columns
+31 −25 · 2 files changed
-
flask-blog.py+15 −1 -
templates/weather-average.html+16 −24
@@ -15,9 +15,23 @@ def user(name): def index(): months = ['January','February','March','April','May','June','July', 'August','September','October','November','December'] + weather = { + 'January': {'min': 38, 'max': 47, 'rain': 6.14}, + 'February': {'min': 38, 'max': 51, 'rain': 4.79}, + 'March': {'min': 41, 'max': 56, 'rain': 4.5}, + 'April': {'min': 44, 'max': 61, 'rain': 3.4}, + 'May': {'min': 49, 'max': 67, 'rain': 2.55}, + 'June': {'min': 53, 'max': 73, 'rain': 1.69}, + 'July': {'min': 57, 'max': 80, 'rain': 0.59}, + 'August': {'min': 58, 'max': 80, 'rain': 0.71}, + 'September': {'min': 54, 'max': 75, 'rain': 1.54}, + 'October': {'min': 48, 'max': 63, 'rain': 3.42}, + 'November': {'min': 41, 'max': 52, 'rain': 6.74}, + 'December': {'min': 36, 'max': 45, 'rain': 6.94} + } return render_template('weather-average.html',city='Portland, OR', - months=months) + months=months,weather=weather) if __name__ == '__main__': app.run(debug=True) @@ -1,27 +1,19 @@ -<!DOCTYPE html> -<html> -<head lang="en"> - <meta charset="UTF-8"> - <title></title> -</head> -<body> - <h2>{{ city }}</h2> - <hr> - <table> - <tr> - <th>Month</th> - <th>Min</th> - <th>Max</th> - <th>Rainfall</th> - </tr> - {% for month in months %} +<h1>Weather Averages</h1> +<h2>{{ city }}</h2> +<hr> +<table> + <tr> + <th>Month</th> + <th>Min</th> + <th>Max</th> + <th>Rainfall</th> + </tr> + {% for month in months %} <tr> <td>{{ month }}</td> - <td></td> - <td></td> - <td></td> + <td>{{ weather[month]['min'] }}</td> + <td>{{ weather[month]['max'] }}</td> + <td>{{ weather[month]['rain'] }}</td> </tr> - {% endfor %} - </table> -</body> -</html> \ No newline at end of file + {% endfor %} +</table> \ No newline at end of file