-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (60 loc) · 1.83 KB
/
app.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
76
77
# import necessary libraries
import os
from flask import (
Flask,
render_template,
jsonify,
request,
redirect)
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Database Setup
#################################################
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', '') or "sqlite:///db.sqlite"
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', '')
db = SQLAlchemy(app)
from .models import Pet
# create route that renders index.html template
@app.route("/")
def home():
return render_template("index.html")
# Query the database and send the jsonified results
@app.route("/send", methods=["GET", "POST"])
def send():
if request.method == "POST":
name = request.form["petName"]
lat = request.form["petLat"]
lon = request.form["petLon"]
pet = Pet(name=name, lat=lat, lon=lon)
db.session.add(pet)
db.session.commit()
return redirect("/", code=302)
return render_template("form.html")
@app.route("/api/pals")
def pals():
results = db.session.query(Pet.name, Pet.lat, Pet.lon).all()
hover_text = [result[0] for result in results]
lat = [result[1] for result in results]
lon = [result[2] for result in results]
pet_data = [{
"type": "scattergeo",
"locationmode": "USA-states",
"lat": lat,
"lon": lon,
"text": hover_text,
"hoverinfo": "text",
"marker": {
"size": 50,
"line": {
"color": "rgb(8,8,8)",
"width": 1
},
}
}]
return jsonify(pet_data)
if __name__ == "__main__":
app.run()