File size: 1,120 Bytes
8621d18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import random
from pathlib import Path
import flask
app = flask.Flask(__name__)
@app.get('/')
def index():
return flask.render_template('index.html')
@app.get('/weather/data')
def weather_data():
data = get_random_report()
return flask.jsonify(data)
@app.get('/serviceWorker.js')
def worker():
js = Path(__file__).parent / 'static' / 'js' / 'serviceWorker.js'
text = js.read_text()
resp = flask.make_response(text)
resp.content_type = 'application/javascript'
resp.headers['Service-Worker-Allowed'] = '/'
return resp
def get_random_report() -> dict:
reports = [
{
'report': 'Clear and beautify skies today.',
'sky': 'sunny',
'temp': random.randint(65, 88),
},
{
'report': 'Cloudy and a bit cool today.',
'sky': 'cloudy',
'temp': random.randint(48, 60),
},
{
'report': 'Are those icicles falling from the sky? Get inside.',
'sky': 'rain',
'temp': random.randint(33, 40),
}
]
return random.choice(reports)
|