-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (78 loc) · 3.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pandas as pd
import datetime as dt
from wtforms import Form, StringField
from flask_wtf import FlaskForm
import re, json
from flask import (
Flask,
request,
render_template,
jsonify)
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
engine = create_engine("sqlite:///db/events.sqlite")
# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# Create our session (link) from Python to the DB
session = Session(engine)
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/web_structure')
def web_structure():
return render_template('web_structure.html')
# class ArtistSearch(FlaskForm):
@app.route('/artist', methods=['GET','POST'])
def artist():
#form = ArtistSearch(request.form)
geojson = {}
artist = ''
#if request.method == 'POST':
m = re.search('artistName=(\w+)', str(request))
if m :
artist = m.group(1)
artist_events = Base.classes.artist_events
results = session.query(artist_events.artist_name, artist_events.city, artist_events.consert_name, artist_events.date, artist_events.lat, artist_events.lng, artist_events.popularity).filter(artist_events.artist_name == artist).all()
geojson = to_geojson(results)
geojson = json.dumps(geojson)
return render_template('artist.html',results=artist)
@app.route('/api/<artist>')
def api(artist):
artist_events = Base.classes.artist_events
results = session.query(artist_events.artist_name, artist_events.city, artist_events.consert_name, artist_events.date, artist_events.lat, artist_events.lng, artist_events.popularity).filter(artist_events.artist_name == artist).all()
return jsonify(results)
@app.route('/api_geojson/<artist>')
def api_geojson(artist):
artist_events = Base.classes.artist_events
results = session.query(artist_events.artist_name, artist_events.city, artist_events.consert_name, artist_events.date, artist_events.lat, artist_events.lng, artist_events.popularity).filter(artist_events.artist_name == artist).all()
geojson = to_geojson(results)
print("Loading GeoJSON...")
return jsonify(geojson)
# Use the following functions to convert api info to GeoJSON
def to_geojson(results):
geojson = {'type':'FeatureCollection', 'features':[]}
for result in results:
feature = {'type':'Feature',
'properties':{},
'geometry':{'type':'Point',
'coordinates':[]}}
feature['geometry']['coordinates'] = [result.lng,result.lat]
feature['properties']['name'] = result.consert_name
feature['properties']['date'] = result.date
feature['properties']['city'] = result.city
feature['properties']['popularity'] = result.popularity
geojson['features'].append(feature)
return geojson
if __name__ == '__main__':
app.run(debug=True)