Skip to content

Commit

Permalink
Merge pull request charleyferrari#1 from charleyferrari/new_flask_api
Browse files Browse the repository at this point in the history
New flask api
  • Loading branch information
charleyferrari authored Oct 30, 2018
2 parents 20a651c + e2c65fc commit c0bde69
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.DS_Store
*.ipynb_checkpoints
52 changes: 52 additions & 0 deletions module4.5/complex-api/hpi_api.py
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)
3 changes: 3 additions & 0 deletions module4.5/complex-api/js/index.js
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);
});
8 changes: 8 additions & 0 deletions module4.5/complex-api/templates/index.html
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>

0 comments on commit c0bde69

Please sign in to comment.