-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
80 lines (63 loc) · 2.03 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
import datetime
import requests
import pandas as pd
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.resources import CDN
from bokeh.embed import file_html
from flask import Flask, render_template, request
app = Flask(__name__)
def get_plot(symbol, feature):
# symbol = "GOOG"
# feature = "close"
alpha_path = "https://www.alphavantage.co/query?"
function = "function=TIME_SERIES_DAILY_ADJUSTED"
symbol = "&symbol=" + symbol
size = "&outputsize=full&datatype=json"
api_key = "&apikey=FBSJ78DDZA5GWDKW"
api_request = alpha_path + function + symbol + size + api_key
r = requests.get(api_request)
response = r.json()
data = pd.DataFrame.from_dict(
response["Time Series (Daily)"],
orient="index"
)
data = data.sort_index(axis=1)
data = data.rename(columns={col: col[3:] for col in data.columns})
df = data.astype({
'open': 'float',
'high': 'float',
'low': 'float',
'close': 'float',
'adjusted close': 'float',
'volume': 'int32'
})
df["date"] = pd.to_datetime(df.index)
dft = df[["date", feature]]
# numlines = len(dft.columns - 1)
source = ColumnDataSource(dft)
p = figure(x_axis_type="datetime")
p.line(source=source, x="date", y=feature)
p.title.text = "Daily Stock Price"
p.xaxis.axis_label = "Date"
p.yaxis.axis_label = feature
return p
def make_html(plot):
html = file_html(plot, CDN, "myplot")
f = open("./templates/output.html", "w")
f.write(html)
f.close()
@app.route('/')
def index():
return render_template('input.html')
@app.route('/output', methods=["POST"])
def output():
symbol = request.form["ticker"]
feature = request.form["features"]
result_plot = get_plot(symbol, feature)
make_html(result_plot)
return render_template('output.html')
# script, div = components(result_plot)
# return render_template("output.html", script=script, div=div)
if __name__ == '__main__':
app.run(port=33507)