-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
75 lines (47 loc) · 1.78 KB
/
server.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from flask import Flask, request, render_template
from flask_debugtoolbar import DebugToolbarExtension
import json, requests, os, urllib2
from pprint import pprint
app = Flask(__name__)
# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"
@app.route("/")
def index():
"""Return home page."""
return render_template("index.html")
@app.route("/get-weather", methods=['GET'])
def getWeatherPy():
"""Gets local weather from API"""
lat = request.args.get("lat")
lon = request.args.get("lon")
# make API call to weather site and return response Python object
response = requests.get("https://fcc-weather-api.glitch.me/api/current?lat=%s&lon=%s" % (lat, lon))
# convert response object to python dict
# dict used to work with data, but not send over internet
data = response.json()
# pprint(data)
weather = data['weather'][0]['description']
icon = data['weather'][0]['icon']
humidity = data['main']['humidity']
temp = data['main']['temp']
# print weather, humidity, temp
results = [weather, icon, humidity, temp]
# converts dictionary to string so that it can be sent over http and displayed
# as result on local server
return json.dumps(results)
@app.route("/error")
def error():
raise Exception("Error!")
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
# app.debug = True
# Use the DebugToolbar
# DebugToolbarExtension(app)
# import sys
# if sys.argv[-1] == "jstest":
# JS_TESTING_MODE = True
PORT = int(os.environ.get("PORT", 5000))
DEBUG = "NO_DEBUG" not in os.environ
app.run(host="0.0.0.0", port=PORT, debug=DEBUG)
app.run(host="0.0.0.0")