forked from charleyferrari/CUNY_DATA_608
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request charleyferrari#1 from charleyferrari/new_flask_api
New flask api
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.DS_Store | ||
*.ipynb_checkpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from flask import Flask, jsonify, send_from_directory, render_template | ||
import pandas as pd | ||
|
||
|
||
app = Flask(__name__) | ||
|
||
|
||
# This is an API meant to serve some housing price index data | ||
@app.route('/hpi/<string:seasonality>/<string:metro>') | ||
def return_hpi_data(seasonality, metro): | ||
|
||
# Read in raw data | ||
raw_data = pd.read_csv( | ||
'https://raw.githubusercontent.com/charleyferrari/CUNY_DATA608/master/lecture3/Sample%20Code/hpi.csv') | ||
|
||
# Filter based on seasonality and metro | ||
filtered_data = raw_data.loc[(raw_data['Seasonality'] == seasonality) & (raw_data['Metro'] == metro), | ||
['DATE', 'Tier', 'HPI']] | ||
|
||
# Deal with datetime | ||
filtered_data['DATE'] = pd.to_datetime(filtered_data['DATE']) | ||
|
||
# Pivot so it's easier to build our json. Data now looks like this: | ||
# | Date | High | Middle | Low | | ||
# (Date is the index) | ||
filtered_data = filtered_data.pivot(columns='Tier', index='DATE', values='HPI') | ||
|
||
# Build our json, then return it with jsonify | ||
filtered_data_json = { | ||
'Date': filtered_data.index.map(lambda x: x.strftime(format='%Y-%m')).tolist(), | ||
'High': filtered_data['High'].tolist(), | ||
'Middle': filtered_data['Middle'].tolist(), | ||
'Low': filtered_data['Low'].tolist() | ||
} | ||
|
||
return jsonify(filtered_data_json) | ||
|
||
|
||
# This routing allows us to view index.html | ||
@app.route('/') | ||
def index(): | ||
return render_template('index.html') | ||
|
||
|
||
# This routing allows us to load local Javascript | ||
@app.route('/js/<path:path>') | ||
def send_js(path): | ||
return send_from_directory('js', path) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
d3.json('hpi/NSA/San Francisco'.replace(' ', '%20'), function(json) { | ||
console.log(json); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<body> | ||
<p>This is a test</p> | ||
<script src="https://d3js.org/d3.v4.min.js"></script> | ||
<script src="js/index.js"></script> | ||
</body> | ||
</html> |