-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
153 lines (106 loc) · 4.6 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
from flask import Flask, render_template, request, redirect, flash
from flask_wtf import FlaskForm as Form
from wtforms import FieldList, FormField, StringField, TextField, FloatField, IntegerField, BooleanField, TextAreaField, SubmitField, RadioField, SelectField, DateField, validators
from wtforms.fields.html5 import IntegerRangeField
from stock_data import getPlotData, createFigure, filterDatabyRange
import requests
import pandas as pd
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models.sources import ColumnDataSource
yaxis_choices = [('open', 'Opening Price'),
('close', 'Closing Price'),
('high', 'High Price'),
('low', 'Low Price'),
('adj_open', 'Adjusted Opening Price'),
('adj_close', 'Adjusted Closing Price'),
('adj_high', 'Adjusted High Price'),
('adj_low', 'Adjusted Low Price'),
]
app = Flask(__name__)
month_choices = [(1, 'January'), (2, 'February'), (3, 'March'), (4, 'April'),
(5, 'May'), (6, 'June'), (7, 'July'), (8,
'August'), (9, 'September'),
(10, 'October'), (11, 'November'), (12, 'December')]
errormsg = [
(0, 0), ('No records found within selection range. Displaying all data.', 'warning'),
('Unable to make request.', 'danger'),
('Could not find stock ticker.', 'danger'),
]
app.secret_key = os.environ.get("FLASK_SECRET_KEY", "you-will-never-guess")
api_file = 'apikey.txt'
try:
with open(api_file, 'r') as f:
api = f.read().strip()
except FileNotFoundError:
print('No Quandal API KEY Loaded')
api = os.environ['quandal']
class tickerlookupSubmitForm(Form):
ticker = TextField(
'Ticker', [validators.required()])
class tickerloopup_settingsForm(Form):
ticker = TextField('Ticker')
yaxis = SelectField('Pricing data', choices=yaxis_choices)
yearChoices = [(2018, 2018)]
startMonth = SelectField('Month', choices=month_choices)
startYear = SelectField('Year', choices=yearChoices)
endMonth = SelectField('Month', choices=month_choices)
endYear = SelectField('Year', choices=yearChoices)
@app.route('/', methods=['post', 'get'])
def index():
form = tickerlookupSubmitForm(request.form)
if request.method == 'POST':
ticker = form.ticker.data
return redirect('/lookup/{}'.format(ticker))
else:
return render_template('index.html', form=form)
@app.route('/lookup/<string:id>', methods=['post', 'get'])
def tickerplot(id):
form = tickerloopup_settingsForm(request.form)
error = 0
if request.method == 'POST':
if form.ticker.data != id:
return redirect('/lookup/{}'.format(form.ticker.data), code=307)
data, error = getPlotData(id, api)
# Check for: request and result count
if(error):
flash(errormsg[error][0], errormsg[error][1])
return render_template('lookup.html', id=[], the_div=[], the_script=[], form=form)
yaxis = request.form.get('yaxis')
label = dict(form.yaxis.choices).get(form.yaxis.data)
data2, error = filterDatabyRange(
data, int(form.startYear.data), int(form.startMonth.data), int(form.endYear.data), int(form.endMonth.data),)
# Check for: filter result count
if(error):
flash(errormsg[error][0], errormsg[error][1])
return render_template('lookup.html', id=None, the_div=None, the_script=None, form=form)
else:
data, error = getPlotData(id, api)
# Check for: request and result count
if(error):
flash(errormsg[error][0], errormsg[error][1])
return render_template('lookup.html', id=[], the_div=[], the_script=[], form=form)
yaxis = 'close'
label = 'Closing Price'
data2 = data
form.ticker.default = id
maxyear = data['date'].dt.year.max()
minyear = data['date'].dt.year.min()
choices = []
for year in range(minyear, maxyear+1):
choices.append((year, year))
form.startYear.choices = choices
form.endYear.choices = choices
if request.method == 'GET':
form.endYear.default = maxyear
form.endMonth.default = 12
form.process()
plot = createFigure(data2, yaxis, id, 'Date', label)
script, div = components(plot)
return render_template('lookup.html', id=id, the_div=div, the_script=script, form=form)
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(port=33507, debug=True)