-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
232 lines (181 loc) · 7.17 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
from flask import Flask, render_template, request, url_for, redirect
#from flask_sqlalchemy import SQLAlchemy
#from sqlalchemy.sql import func
import re
from pycoingecko import CoinGeckoAPI
from apscheduler.schedulers.background import BackgroundScheduler
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from io import BytesIO
import base64
import pfunctions
from db import db
from currency import Currency as Currency
from graph import Graph as Graph
from currency import Total
from graph import Getdate
from graph import Getvalue
app = Flask(__name__, static_url_path='/static')
db.init_app(app)
cg = CoinGeckoAPI()
coinlist = cg.get_coins_list()
#https://www.digitalocean.com/community/tutorials/how-to-use-flask-sqlalchemy-to-interact-with-databases-in-a-flask-application
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_DATABASE_URI'] =\
'sqlite:///' + os.path.join(basedir, 'database.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#Creating the Database via the model
#export FLASK_APP=main
#flask shell
#>>> from main import db, Currency
#>>> db.create_all()
#>>> Currency.query.all()
#https://stackoverflow.com/questions/21214270/how-to-schedule-a-function-to-run-every-hour-on-flask
# save the total balance one time per day 24h
def sensor():
""" Function for test purposes. """
print("Scheduler is alive!")
addvalue = Graph(totalvalue=Total())
db.session.add(addvalue)
db.session.commit()
sched = BackgroundScheduler(daemon=True)
sched.add_job(sensor, 'interval', minutes=1440)
sched.start()
@app.route('/')
def index():
Getdate()
Getvalue()
currency = Currency.query.all()
return render_template('index.html',
currency=currency,
getprice=pfunctions.Getprice,
getname=pfunctions.Getname,
geticon=pfunctions.Geticon,
iconvariation=pfunctions.Iconvariation,
valuevariation=pfunctions.Valuevariation,
getsymbol=pfunctions.Getsymbol,
total=Total)
# ...
# to creat a transaction
@app.route('/create/', methods=('GET', 'POST'))
def create():
coinlist = cg.get_coins_list()
if request.method == 'POST':
idcurrency = request.form['idcurrency']
price = (request.form['price'])
quantity = (request.form['quantity'])
currency = Currency(idcurrency=idcurrency,
price=price,
quantity=quantity)
db.session.add(currency)
db.session.commit()
return redirect(url_for('index'))
return render_template('create.html', **locals())
# ...
# ...
# to creat a page for eache transaction
@app.route('/<int:currency_id>/')
def currency(currency_id):
currency = Currency.query.get_or_404(currency_id)
return render_template('currency.html',
currency=currency,
getname=pfunctions.Getname,
getsymbol=pfunctions.Getsymbol,
geticon=pfunctions.Geticon)
# ...
# to edit a transaction
@app.route('/edit/', methods=('GET', 'POST'))
def edit():
db_currency = Currency.query.all()
item = request.form.get('idcurrency')
# item = db.session.query(Currency).filter(
#Currency.idcurrency == idcurrency).first()
print('edit : ', item)
if request.method == 'POST':
currency = Currency.query.get_or_404(item)
print('edit : ', item)
#idcurrency = request.form['idcurrency']
price = (request.form['price'])
quantity = (request.form['quantity'])
#currency.idcurrency = idcurrency
currency.price = price
currency.quantity = quantity
db.session.add(currency)
db.session.commit()
return redirect(url_for('index'))
return render_template('edit.html',
db_currency=db_currency,
getsymbol=pfunctions.Getsymbol,
getname=pfunctions.Getname)
# ...
#to delet a transaction
@app.route('/delete_page/', methods=('GET', 'POST'))
def delete_page():
xcurrency_idcurrency = request.form.get('currency_idcurrency')
print('url id : ', xcurrency_idcurrency, type(xcurrency_idcurrency))
parsed_currency_idcurrency = re.split('----', str(xcurrency_idcurrency))[0]
print('url xid : ', parsed_currency_idcurrency,
type(parsed_currency_idcurrency))
currency = Currency.query.all()
if request.method == 'POST':
#https://stackoverflow.com/questions/36972044/sqlalchemy-select-id-from-table-1-where-name-xyz/36997324
item = db.session.query(Currency).filter(
Currency.idcurrency == parsed_currency_idcurrency).first()
del_currency = Currency.query.get_or_404(item.id)
db.session.delete(del_currency)
db.session.commit()
return redirect(url_for('index'))
return render_template('delete_page.html',
currency=currency,
getname=pfunctions.Getname,
getsymbol=pfunctions.Getsymbol,
getprice=pfunctions.Getprice)
#https://stackoverflow.com/questions/20107414/passing-a-matplotlib-figure-to-html-flask
# to creat a figure with matplotlib
@app.route('/graph/')
def graph():
img = BytesIO()
#https://pythonguides.com/matplotlib-change-background-color/
plt.figure(facecolor='black', figsize=(12,6))
plt.rcParams.update({'axes.facecolor': 'black'})
#plt.figure(figsize=(12,6))
#https://stackoverflow.com/questions/9627686/plotting-dates-on-the-x-axis-with-pythons-matplotlib
ax = plt.axes()
#https://stackoverflow.com/questions/25689238/show-origin-axis-x-y-in-matplotlib-plot
ax.spines['bottom'].set_position('zero')
x = Getdate()
y = Getvalue()
#ax.spines['bottom'].set_color('yellow')
plt.plot(x, y, color='green', linewidth=4.0)
plt.gcf().autofmt_xdate()
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
#https://www.adamsmith.haus/python/answers/how-to-change-the-color-of-the-axes-of-a-plot-in-matplotlib-in-python
#changing ax color
ax.spines["bottom"].set_color("white")
ax.spines["top"].set_color("black")
ax.spines["left"].set_color("white")
#https://stackoverflow.com/questions/14165344/matplotlib-coloring-axis-tick-labels
#changing label color
label = plt.xlabel("date",
fontweight ='bold',
fontsize=16,
loc="right")
label.set_color("white")
label = plt.ylabel("Euro €",
fontweight ='bold',
fontsize=16,
loc="center")
label.set_color("white")
#saving the figure as image
plt.savefig(img, format='png')
plt.close()
img.seek(0)
#builing the URL of the figure
graph_url = base64.b64encode(img.getvalue()).decode('utf8')
print(len(graph_url))
return render_template('graph.html', graph_url=graph_url)
app.run(host='0.0.0.0', port=8080)