/
app.py
134 lines (113 loc) · 4.12 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
Created on Thu May 26 22:05:49 2016
@author: Adam
"""
'''
uses embed.html and flask to render an http call to yahoo for stock quotes
'''
from flask import Flask, render_template, request
from bokeh.embed import components
#from bokeh.plotting import figure
from bokeh.resources import INLINE
from bokeh.templates import JS_RESOURCES, CSS_RESOURCES
from bokeh.util.string import encode_utf8
from dateutil.parser import parse
import dateutil.relativedelta
#, relativedelta
from datetime import datetime, date
from bokeh.charts import TimeSeries, output_file, vplot
YOURAPIKEY="WQxVvsi1gEbN8yfbQxUH"
import pandas
import requests
app = Flask(__name__)
colors = {
'Black': '#000000',
'Red': '#FF0000',
'Green': '#00FF00',
'Blue': '#0000FF',
}
def getitem(obj, item, default):
if item not in obj:
return default
else:
return obj[item]
@app.route("/")
def nowbegin():
edate = date.today().strftime("%Y-%m-%d")
sdate = date.today()-dateutil.relativedelta.relativedelta(months=1)
return(graphstocks("AAPL",sdate,edate,"Black"))
def graphstocks(ssymbol, sdate, edate,color):
stock = ssymbol
if color not in colors:
color="Black"
api_url='https://www.quandl.com/api/v3/datasets/WIKI/%(symbol)s.json?api_key=%(key)s&start_date=%(sdate)s&end_date=%(edate)s' % {"symbol":stock, "key":YOURAPIKEY,"sdate":sdate, "edate":edate}
session = requests.Session()
session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3))
raw_data=session.get(api_url)
aapl_stock=raw_data.json()
color="Black"
cnames = aapl_stock['dataset']['column_names']
df = pandas.DataFrame(aapl_stock['dataset']['data'],columns=cnames) # create dataframe and assign column names
df['Date']=pandas.to_datetime(df['Date']) # convert Date column to DateTime in place
tsline = TimeSeries(df,x='Date', y='Close', ylabel=ssymbol+' Stock Prices', legend=True, color=colors[color])
fig=vplot(tsline)
# Configure resources to include BokehJS inline in the document.
# For more details see:
# http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed
js_resources = JS_RESOURCES.render(
js_raw=INLINE.js_raw,
js_files=INLINE.js_files
)
css_resources = CSS_RESOURCES.render(
css_raw=INLINE.css_raw,
css_files=INLINE.css_files
)
#from: http://bokeh.pydata.org/en/0.11.1/docs/releases/0.11.0.html
# Before:
#html = file_html(layout, None, title=title, template=template, js_resources=js_resources, css_resources=css_resources)
#v0.11:
#
#html = file_html(layout, resources=(js_resources, css_resources), title=title, template=template)
# For more details see:
# http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components
script, div = components(fig, INLINE)
html = render_template(
'embed.html',
plot_script=script,
plot_div=div,
js_resources=js_resources,
css_resources=css_resources,
color=color,
_from=sdate,
to=edate,
symbol_lulu=ssymbol
)
return encode_utf8(html)
@app.route("/butpush", methods=['GET','POST'])
def butpush():
if request.method =='GET':
ssymbol="AAPL"
edate = date.today().strftime("%Y-%m-%d")
sdate = date.today()-dateutil.relativedelta.relativedelta(months=1)
return(graphstocks(ssymbol,sdate,edate,"Black"))
else:
ssymbol=request.form['symbol_lulu']
sdate = request.form['_from']
edate = request.form['to']
#color = request.form['color']
color = "Black"
try:
sdate = parse(sdate).strftime("%Y-%m-%d")
edate = parse(edate).strftime("%Y-%m-%d")
except ValueError:
edate = date.today().strftime("%Y-%m-%d")
sdate = date.today()-dateutil.relativedelta.relativedelta(months=1)
return(graphstocks(ssymbol,sdate,edate,color))
def main():
#app.debug = True
#app.run()
app.run(port=33507) #before putting on heroku, take away debug and use port 33507
#print "HI""hi"
if __name__ == "__main__":
main()