forked from thedataincubator/flask-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
51 lines (40 loc) · 1.67 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
from flask import Flask, render_template, request, redirect
import requests
import simplejson as json
import pandas as pd
from bokeh.plotting import figure
from bokeh.embed import components
app = Flask(__name__)
@app.route('/')
def main():
return redirect('/index')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/graph', methods=["GET", "POST"])
def graph():
form_dict = {}
if request.method == 'POST':
# Extract form info and handle errors
ticker = request.form['ticker']
if not ticker:
return '<h1>Please go back and enter a stock ticker</h1>'
features = request.form.getlist('features')
if not features:
return '<h1>Please go back and select some features to show</h1>'
# Call the Quandl API
api_url = 'https://www.quandl.com/api/v1/datasets/WIKI/%s.json?api_key=bhDkb5WTxo-gXcFN5mgq' % ticker
response = requests.get(api_url)
json_response = json.loads(response.content)
# Put the data in a Pandas dataframe
data = pd.DataFrame(json_response['data'], columns=json_response['column_names'])
data['Date'] = pd.to_datetime(data['Date'])
# Send the data to the Bokeh plot
p = figure(x_axis_type="datetime", width=800, height=600)
line_color=['red', 'green', 'blue', 'brown']
for index, feature in enumerate(features):
p.line(data['Date'], data[feature], legend=feature, line_color = line_color[index])
script, div = components(p)
return render_template('graph.html', script=script, div=div, company = ticker.upper())
if __name__ == '__main__':
app.run(port=5000)