-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
123 lines (101 loc) · 3.14 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
import sqlite3
import datetime
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.resources import INLINE
from flask import Flask, render_template
from data import get_rates, get_lowest_rate, add_data
app = Flask(__name__)
@app.route('/')
def index():
rates = get_rates()
lowest = get_lowest_rate(rates)
historical = get_historical_data()
chart = create_chart(historical)
# grab the static resources
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
# render template
script, div = components(chart)
return render_template(
'index.html',
points=len(historical),
rates=lowest,
plot_script=script,
plot_div=div,
js_resources=js_resources,
css_resources=css_resources,
)
def get_historical_data():
all_data = []
with sqlite3.connect('bitcoin.db') as connection:
c = connection.cursor()
c.execute("""SELECT * FROM rates""")
rows = c.fetchall()
for value in rows:
all_data.append({
'currency': value[0],
'price': value[1],
'time': value[2]
})
return all_data
def create_chart(data):
data.sort(key=lambda d: d['time'])
# create figure
p = figure(
x_axis_label='time',
y_axis_label='price',
x_axis_type="datetime",
width=1000,
height=500
)
# get x and y axis data
curr_1_x = []
curr_1_y = []
curr_2_x = []
curr_2_y = []
curr_3_x = []
curr_3_y = []
for value in data:
time = datetime.datetime.strptime(
value['time'], '%Y-%m-%d %H:%M:%S')
if value['currency'] == 'bitstamp':
curr_1_x.append(time)
curr_1_y.append(value['price'])
if value['currency'] == 'kraken':
curr_2_x.append(time)
curr_2_y.append(value['price'])
if value['currency'] == 'bittrex':
curr_3_x.append(time)
curr_3_y.append(value['price'])
p.line(curr_1_x, curr_1_y, color='red', legend='bitstamp', line_width=2)
p.line(curr_2_x, curr_2_y, color='green', legend='kraken', line_width=2)
p.line(curr_3_x, curr_3_y, color='black', legend='bittrex', line_width=2)
p.legend.location = 'bottom_left'
return p
def chart():
all_data = defaultdict(list)
query = models.Currency.query.all()
p = figure(
width=1080,
height=600,
x_axis_type="datetime",
)
for row in query:
all_data[row.exchange].append((row.horah, row.price))
for i, (exchange, points) in enumerate(all_data.items()):
color = bokeh.palettes.Category20[20][i]
X,Y = zip(*sorted(points))
p.line(X,Y, line_width=2, alpha=0.7, legend=exchange, color=color)
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
script, div = components(p)
return render_template(
'index.html',
js_resources = js_resources,
css_resources = css_resources,
plot_script = script,
plot_div = div,
)
if __name__ == '__main__':
app.run(debug=True)