/
app.py
65 lines (50 loc) · 1.77 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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 31 00:13:30 2015
@author: vaidyanatk
"""
# Imports
from flask import Flask, render_template, request, redirect
from pandas import DataFrame, to_datetime
from datetime import datetime,timedelta
from bokeh.plotting import figure
from bokeh import embed
import requests
import os
app = Flask(__name__)
selector = {}
@app.route('/')
def main():
return redirect('/index')
@app.route('/index', methods=['GET','POST'])
def index():
return render_template('index.html')
def make_plot():
types = request.form.getlist('type')
ticker = request.form['ticker']
now = datetime.now()
end_date = now.strftime('%Y-%m-%d')
start_date = (now - timedelta(days = 180)).strftime('%Y-%m-%d') # six - month timeframe
URL = 'https://www.quandl.com/api/v3/datasets/WIKI/'+ticker+'.json?start_date='+start_date+'&end_date='+end_date+'&order=asc&api_key=eFoXAcyvLhyuB3Rsvg6o'
r = requests.get(URL)
df_handle = DataFrame(r.json())
df = DataFrame(df_handle.ix['data','dataset'], columns = df_handle.ix['column_names','dataset'])
df.columns = [x.lower() for x in df.columns]
df = df.set_index(['date'])
df.index = to_datetime(df.index)
p = figure(x_axis_type = "datetime")
if 'open' in types:
p.line(df.index, df['open'], color='blue', legend='opening price')
if 'high' in types:
p.line(df.index, df['high'], color='red', legend='highest price')
if 'close' in types:
p.line(df.index, df['close'], color='green', legend='closing price')
return p
@app.route('/chart',methods=['GET','POST'])
def chart():
plot = make_plot()
script, div = embed.components(plot)
return render_template('stock_plot.html', script = script, div = div)
if __name__ == '__main__':
port = int(os.environ.get("PORT", 8000))
app.run(host='0.0.0.0', port = port)