-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
50 lines (38 loc) · 1.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
try:
from flask.ext.cors import CORS
from flask import Flask
import pandas as pd
import json
except ImportError as error:
print error
print '\nPlease run this command resolve this error :'
print '\tpip install flask flask-cors pandas'
sys.exit()
APP = Flask(__name__)
VARS = {}
def prepare_data():
data = pd.read_csv('all_earthquakes.csv', low_memory=False)
data['year'] = data['time'].apply(lambda x: x[:4])
VARS['data'] = data
return True
prepare_data()
@APP.route('/earthquakes', methods=['GET'])
def earthquakes_by_year():
"""Return The trend per year"""
data = VARS['data']
grouped = data.groupby('year')
count = grouped['mag'].count()
return count.to_csv(None, index=False)
@APP.route('/earthquakes/<year>', methods=['GET'])
def earthquakes_in_year(year='2015'):
"""Filter the dataset by year and return in csv format"""
data = VARS['data']
filtered_data = data[data['year'] == year]
return filtered_data.to_csv(None, index=False)
@APP.route('/', methods=['GET'])
def dashboard():
"""Return the HTML for the dashboard"""
return None
if __name__ == "__main__":
CORS(APP)
APP.run(debug=True)