up
This commit is contained in:
80
flask_sample/app.py
Normal file
80
flask_sample/app.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import json
|
||||
from flask import Flask, render_template, request
|
||||
from datetime import datetime, date
|
||||
|
||||
import requests
|
||||
|
||||
url = 'https://covidtrackerapi.bsg.ox.ac.uk/api/v2/stringency/actions/'
|
||||
'''
|
||||
01. Belarus - BLR
|
||||
02. China - CHN
|
||||
03. France - FRA
|
||||
04. Germany - DEU
|
||||
05. India - IND
|
||||
06. Israel - ISR
|
||||
07. Russia - RUS
|
||||
08. Serbia - SRB
|
||||
09. Spain - ESP
|
||||
10. Turkey - TUR
|
||||
'''
|
||||
countries = ['blr', 'chn', 'fra', 'deu', 'ind', 'isr', 'rus', 'srb', 'esp', 'tur']
|
||||
tmp_output = {}
|
||||
|
||||
|
||||
def check_date(input_date):
|
||||
""" Check date format from input string.
|
||||
|
||||
Args: input_date - (str).
|
||||
Return: Bool - True if date correct. """
|
||||
try:
|
||||
# valid_date = datetime.strptime(input_date, '%Y-%m-%d').date()
|
||||
datetime.strptime(input_date, '%Y-%m-%d').date()
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def request_one_country(api, cntr, day):
|
||||
""" Request from api-url.
|
||||
|
||||
Args: api - (str) - api url,
|
||||
cntr - (str) country alpha-3 code,
|
||||
day - (int) YYYY-MM-DD.
|
||||
Return: Response - (dict). """
|
||||
r_out = requests.get(f'{api}{cntr}/{day}')
|
||||
return r_out.json()
|
||||
|
||||
######################################################
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/', methods=['post', 'get'])
|
||||
def index():
|
||||
if request.method == 'POST':
|
||||
input_date = request.form.get('input_date') # запрос к данным формы
|
||||
if check_date(input_date):
|
||||
correct_date = datetime.strptime(input_date, '%Y-%m-%d').date()
|
||||
if correct_date >= date.today():
|
||||
return render_template('index.html', message='Input date has not arrived yet')
|
||||
|
||||
for country in countries:
|
||||
out = request_one_country(url, country, correct_date)
|
||||
tmp_output.setdefault(country, out.get('stringencyData'))
|
||||
|
||||
output = {input_date: tmp_output}
|
||||
with open('../temp_out.json', 'w') as f:
|
||||
json.dump(output, f, sort_keys=True, indent=2)
|
||||
|
||||
return render_template('index.html')
|
||||
elif not input_date:
|
||||
return render_template('index.html', message='Please input date')
|
||||
else:
|
||||
return render_template('index.html', message='Incorrect input')
|
||||
else:
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host='127.0.0.1', port=8181, debug=True)
|
||||
BIN
flask_sample/static/img/covid19.png
Normal file
BIN
flask_sample/static/img/covid19.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
BIN
flask_sample/static/img/logo.png
Normal file
BIN
flask_sample/static/img/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
3
flask_sample/static/style.css
Normal file
3
flask_sample/static/style.css
Normal file
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
color: blue;
|
||||
}
|
||||
30
flask_sample/templates/index.html
Normal file
30
flask_sample/templates/index.html
Normal file
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>COVID-19 Some statistics</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<p>Some statistics</p>
|
||||
<img src="{{ url_for('static', filename='img/covid19.png') }}"
|
||||
alt="Img" width="300">
|
||||
<!-- alt="Img" width="300" height="300">-->
|
||||
|
||||
<form action="" method="post">
|
||||
<p>
|
||||
<label for="input_date">Input date (YYYY-MM-DD)</label>
|
||||
<input type="text" name="input_date">
|
||||
<input type="submit" value="Show">
|
||||
</p>
|
||||
<!-- <p>-->
|
||||
<!-- <input type="submit" value="Show">-->
|
||||
<!-- </p>-->
|
||||
</form>
|
||||
{{ message }}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
<!--<script>-->
|
||||
<!-- alert('Эй!')-->
|
||||
<!--</script>-->
|
||||
Reference in New Issue
Block a user