-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgui_modules.py
692 lines (619 loc) · 21 KB
/
gui_modules.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
import tkinter as tk
import pathlib
import requests
import re
import finplot as fplt
import matplotlib.pyplot as plt
import yfinance as yf
from datetime import datetime
from ttkthemes import ThemedTk
from tkinter import messagebox
from tkinter import ttk
API_ENDPOINT = "http://127.0.0.1:5000/"
def verify_email(email):
"""
This function validates the given email using regular expression.
Args:
email: Email to be verified.
Returns:
True if email format is correct otherwise False.
"""
email_rule = r"^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$"
return re.search(email_rule, email)
class LoginFrame(ttk.Frame):
"""This class provides styled Login Frame"""
def __init__(self, master):
"""
Constructor for LoginFrame
Args:
master: Parent widget.
Returns:
None
"""
ttk.Frame.__init__(self, master)
self._master = master
ttk.Label(text="Username: ").grid(
row=0, column=0, padx=(50, 0), pady=(50, 5)
)
self.username_entry = ttk.Entry()
self.username_entry.grid(row=0, column=1, padx=(0, 50), pady=(50, 0))
ttk.Label(text="Password: ").grid(
row=1, column=0, padx=(50, 0), pady=(5, 5)
)
self.password_entry = ttk.Entry(show="•")
self.password_entry.grid(row=1, column=1, padx=(0, 50), pady=(5, 5))
ttk.Button(text="Login", command=self.init_login).grid(
row=2, column=0, padx=(50, 0), pady=(5, 5)
)
ttk.Button(
text="New User? SignUp",
command=lambda: self._master.switch_frame(
SignUpFrame, "PyStock | SignUp"
),
).grid(row=2, column=1, padx=(0, 50), pady=(5, 5))
themes = [
"yaru",
"classic",
"xpnative",
"scidpink",
"breeze",
"alt",
"scidgreen",
"clearlooks",
"black",
"scidmint",
"equilux",
"adapta",
"winxpblue",
"scidgrey",
"ubuntu",
"kroc",
"keramik",
"scidpurple",
"arc",
"itft1",
"scidsand",
"clam",
"plastik",
"scidblue",
"default",
"smog",
"winnative",
"elegance",
"vista",
"radiance",
"aquativo",
"blue",
]
self.select_theme = ttk.Combobox(self._master)
self.select_theme["values"] = themes
self.select_theme.bind(
"<<ComboboxSelected>>",
lambda _: self._master.change_theme(self.select_theme.get()),
)
ttk.Label(text="Change Theme: ").grid(
row=3, column=0, padx=(50, 0), pady=(5, 50)
)
self.select_theme.current(0)
self.select_theme.grid(row=3, column=1, padx=(0, 50), pady=(5, 50))
def init_login(self):
"""
This function initiates login process and verify credentials with
server using API. This function automatically opens Dashboard Frame if
login is successful.
Args:
None
Returns:
None
"""
if self.password_entry == "":
messagebox.showerror(
"Login Error", "password field can't be empty."
)
return
payload = {
"username": self.username_entry.get(),
"password": self.password_entry.get(),
}
response = requests.post(API_ENDPOINT + "login", payload).json()
if response.get("error") is not None:
messagebox.showerror("Login Error", response["error"])
else:
self._master.switch_frame(
DashboardFrame, "PyStock | Dashboard", response
)
class SignUpFrame(ttk.Frame):
"""This class provides styled SignUp Frame"""
def __init__(self, master):
"""
Constructor for SignUpFrame
Args:
master: Parent widget.
Returns:
None
"""
ttk.Frame.__init__(self, master)
self._master = master
ttk.Label(text="Email: ").grid(
row=0, column=0, padx=(50, 0), pady=(50, 5)
)
self.email_entry = ttk.Entry()
self.email_entry.grid(row=0, column=1, padx=(0, 50), pady=(50, 5))
ttk.Label(text="Name: ").grid(
row=1, column=0, padx=(50, 0), pady=(5, 5)
)
self.name_entry = ttk.Entry()
self.name_entry.grid(row=1, column=1, padx=(0, 50), pady=(5, 5))
ttk.Label(text="Username: ").grid(
row=2, column=0, padx=(50, 0), pady=(5, 5)
)
self.username_entry = ttk.Entry()
self.username_entry.grid(row=2, column=1, padx=(0, 50), pady=(5, 5))
ttk.Label(text="Password: ").grid(
row=3, column=0, padx=(50, 0), pady=(5, 5)
)
self.password_entry = ttk.Entry(show="•")
self.password_entry.grid(row=3, column=1, padx=(0, 50), pady=(5, 5))
ttk.Label(text="Re-enter password: ").grid(
row=4, column=0, padx=(50, 0), pady=(5, 5)
)
self.repassword_entry = ttk.Entry(show="•")
self.repassword_entry.grid(row=4, column=1, padx=(0, 50), pady=(5, 5))
ttk.Button(text="SignUp", command=self.init_signup).grid(
row=5, column=0, columnspan=2, padx=(50, 0), pady=(5, 50)
)
def init_signup(self):
"""
This function initiates sign-up process and register user with server
using API. This function automatically opens Login if sign-up is
successful.
Args:
None
Returns:
None
"""
error_title = "SignUp Error"
if not verify_email(self.email_entry.get()):
messagebox.showerror(error_title, "Invalid email")
return
if (
self.password_entry.get() == ""
or self.name_entry.get() == ""
or self.username_entry.get() == ""
):
messagebox.showerror(error_title, "Some of the fields are empty.")
return
if self.password_entry.get() != self.repassword_entry.get():
messagebox.showerror(error_title, "passwords don't match.")
return
payload = {
"name": self.name_entry.get(),
"username": self.username_entry.get(),
"email": self.email_entry.get(),
"password": self.password_entry.get(),
}
response = requests.post(API_ENDPOINT + "signup", payload).json()
if response.get("error") is not None:
messagebox.showerror(error_title, response["error"])
else:
messagebox.showinfo(
"Registration done",
"You have registered with PyStock successfully. Now click OK"
" to login into PyStock",
)
self._master.switch_frame(LoginFrame, "PyStock | Login")
class DashboardFrame(ttk.Frame):
"""This class provides styled DashboardFrame Frame"""
def __init__(self, master, user_data):
"""
Constructor for DashboardFrame
Args:
master: Parent widget.
user_data: Userdata in JSON format.
Returns:
None
"""
ttk.Frame.__init__(self, master)
self._master = master
self._user_data = user_data
self.info_frame = ttk.LabelFrame(text="User Info")
self.info_frame.rowconfigure(0)
self.info_frame.rowconfigure(1)
self.info_frame.columnconfigure(0)
self.info_frame.columnconfigure(1)
ttk.Label(
self.info_frame, text="Name: " + self._user_data["name"]
).grid(row=0, column=0, sticky="nsew", padx=(5, 10), pady=(5, 0))
ttk.Label(
self.info_frame, text="Email: " + self._user_data["email"]
).grid(row=0, column=1, sticky="nsew", padx=(5, 10), pady=(5, 0))
ttk.Label(
self.info_frame,
text="Balance ($): " + str(round(self._user_data["balance"], 4)),
).grid(row=1, column=0, sticky="nsew", padx=(5, 10), pady=(5, 5))
ttk.Label(
self.info_frame,
text="Total Portfolio Value ($): "
+ str(round(self._user_data["portfolio_value"], 4)),
).grid(row=1, column=1, sticky="nsew", padx=(5, 10), pady=(5, 5))
self.info_frame.grid(
row=0,
column=0,
sticky="nsew",
padx=(5, 5),
pady=(5, 0),
)
self.search_frame = ttk.LabelFrame(text="Search stock")
self.search_frame.rowconfigure(0)
self.search_frame.rowconfigure(1)
self.search_frame.columnconfigure(0)
self.search_frame.columnconfigure(1)
ttk.Label(self.search_frame, text="Enter stock name: ").grid(
row=0, column=0, padx=(5, 0), pady=(5, 0), sticky="nsew"
)
self.search_entry = ttk.Entry(self.search_frame)
self.search_entry.grid(
row=0, column=1, padx=(5, 5), pady=(5, 0), sticky="nsew"
)
ttk.Button(self.search_frame, text="Search", command=self.search).grid(
row=1,
column=0,
columnspan=2,
padx=(5, 5),
pady=(5, 5),
sticky="nsew",
)
self.search_frame.grid(
row=0, column=1, padx=(0, 5), pady=(5, 0), sticky="nsew"
)
self.stock_table_frame = ttk.LabelFrame(text="Your Portfolio")
self.stock_table_frame.rowconfigure(0)
self.stock_table_frame.columnconfigure(0)
self.stock_table = ttk.Treeview(
self.stock_table_frame,
columns=(
"Sr.",
"Name",
"Quantity",
"Investment",
"Current Price",
"State",
),
show="headings",
selectmode="browse",
)
for col in [
"Sr.",
"Name",
"Quantity",
"Investment",
"Current Price",
"State",
]:
self.stock_table.column(col, width=100)
self.stock_table.heading(col, text=col)
self.stock_table.bind("<Double-1>", self.init_sell)
for count, stock in enumerate(self._user_data["portfolio"], start=1):
if (
abs(
stock["cur_price"] * stock["quantity"]
- stock["investment"]
)
<= 0.01
):
state = "BREAK EVEN"
else:
state = (
"PROFIT"
if stock["cur_price"] * stock["quantity"]
> stock["investment"]
else "LOSS"
)
self.stock_table.insert(
"",
"end",
values=(
count,
stock["name"],
stock["quantity"],
round(stock["investment"], 4),
round(stock["cur_price"], 4),
state,
),
tags=(state,),
)
self.stock_table.tag_configure(
"PROFIT", background="#BEF558", foreground="black"
)
self.stock_table.tag_configure(
"LOSS", background="#F3846C", foreground="black"
)
self.stock_table.tag_configure(
"BREAK EVEN", background="#FFFD69", foreground="black"
)
self.stock_table.grid(row=0, column=0, columnspan=3, sticky="nsew")
ttk.Button(
self.stock_table_frame, text="Refresh", command=self.refresh
).grid(row=1, column=0, sticky="nsew")
ttk.Button(
self.stock_table_frame, text="Buy Stocks", command=self.init_buy
).grid(row=1, column=1, sticky="nsew")
ttk.Button(
self.stock_table_frame, text="Analyze", command=self.analyze
).grid(row=1, column=2, sticky="nsew")
ttk.Label(
self.stock_table_frame,
text="Double click on the stock entry to sell that stock.",
).grid(row=2, column=0, columnspan=3)
self.stock_table_frame.grid(
row=1,
column=0,
columnspan=2,
sticky="nsew",
padx=(5, 5),
pady=(5, 5),
)
def search(self):
"""
This function displays given stock's history for the period of 4 years.
Args:
None
Returns:
None
"""
try:
df = yf.download(
self.search_entry.get(),
start="2017-01-01",
end=datetime.today().strftime("%Y-%m-%d"),
)
fplt.candlestick_ochl(df[["Open", "Close", "High", "Low"]])
fplt.plot(df.Close.rolling(50).mean())
fplt.plot(df.Close.rolling(200).mean())
fplt.show()
except IndexError:
messagebox.showerror("Stock Name Error", "Invalid Stock name")
def refresh(self):
"""
This function refreshes dashboard after getting new user data from the
API. This function indirectly uses login to get updated user data.
Args:
None
Returns:
None
"""
payload = {
"username": self._user_data["username"],
"password": self._user_data["password"],
}
response = requests.post(API_ENDPOINT + "login", payload).json()
self._master.switch_frame(
DashboardFrame, "PyStock | Dashboard", response
)
def analyze(self):
"""
This function analyzes user's portfolio and displays it in the form of
piechart.
Args:
None
Returns:
None
"""
labels = []
sizes = []
for stock in self._user_data["portfolio"]:
labels.append(stock["name"])
sizes.append(stock["quantity"] * stock["cur_price"])
_, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
ax.axis("equal")
ax.set_title("Your portfolio distribution according to current prices")
plt.show()
def init_sell(self, event):
"""
This function initiates the selling process by opening a new
SellWindow with selected stock.
Args:
event: Double Click event captured from the tree view.
Returns:
None
"""
item = self.stock_table.item(
self.stock_table.identify("item", event.x, event.y)
)
SellWindow(self, item["values"][1])
def init_buy(self):
"""
This function initiates the buying process by opening a new
BuyWindow.
Args:
None
Returns:
None
"""
BuyWindow(self)
class SellWindow(tk.Toplevel):
"""This class provides Toplevel window to sell stocks"""
def __init__(self, master, stock_name):
"""
Constructor for SellWindow
Args:
master: Parent widget.
stock_name: Stock ticker obtained from parent's (i.e. DashBoard)
treeview.
Returns:
None
"""
tk.Toplevel.__init__(self)
self._master = master
self.stock_name = stock_name
self.title("PyStock | Sell stock")
self.iconphoto(
False, tk.PhotoImage(file=pathlib.Path("assets/icon.png"))
)
self.resizable(0, 0)
tk.Label(self, text="Quantity: ").grid(
row=0, column=0, padx=(50, 5), pady=(50, 5)
)
self.quantity_entry = tk.Entry(self)
self.quantity_entry.grid(row=0, column=1, padx=(0, 50), pady=(50, 5))
tk.Button(self, text="Sell", command=self.sell_stock).grid(
row=1,
column=0,
columnspan=2,
padx=(50, 50),
pady=(0, 50),
sticky="nsew",
)
def sell_stock(self):
"""
This function sell stocks and register it with server using the API.
Args:
None
Returns:
None
"""
try:
payload = {
"username": self._master._user_data["username"],
"action_type": "SELL",
"stock_ticker": self.stock_name,
"quantity": int(self.quantity_entry.get()),
}
assert payload["quantity"] > 0, "Quantity must be positive."
except (ValueError, AssertionError) as e:
messagebox.showerror("Quantity Error", e)
return
response = requests.post(
API_ENDPOINT + "update_portfolio", payload
).json()
if response.get("error") is not None:
messagebox.showerror("Sell Stock Error", response["error"])
else:
self._master._master.switch_frame(
DashboardFrame, "PyStock | Dashboard", response
)
class BuyWindow(tk.Toplevel):
"""This class provides styled SignUp Frame"""
def __init__(self, master):
"""
Constructor for BuyWindow
Args:
master: Parent widget.
Returns:
None
"""
tk.Toplevel.__init__(self)
self._master = master
self.title("PyStock | Buy stock")
self.iconphoto(
False, tk.PhotoImage(file=pathlib.Path("assets/icon.png"))
)
self.resizable(0, 0)
tk.Label(self, text="Stock Name: ").grid(
row=0, column=0, padx=(50, 0), pady=(50, 0)
)
self.name_entry = tk.Entry(self)
self.name_entry.grid(row=0, column=1, padx=(0, 50), pady=(50, 0))
tk.Label(self, text="Quantity: ").grid(
row=1, column=0, padx=(50, 0), pady=(0, 0)
)
self.quantity_entry = tk.Entry(self)
self.quantity_entry.grid(row=1, column=1, padx=(0, 50), pady=(0, 0))
tk.Button(self, text="Buy", command=self.buy_stock).grid(
row=2,
column=0,
columnspan=2,
padx=(50, 50),
pady=(0, 50),
sticky="nsew",
)
def buy_stock(self):
"""
This function buy stocks and register it with server using the API.
Args:
None
Returns:
None
"""
try:
payload = {
"username": self._master._user_data["username"],
"action_type": "BUY",
"stock_ticker": self.name_entry.get(),
"quantity": int(self.quantity_entry.get()),
}
assert payload["quantity"] > 0, "Quantity must be positive."
except (ValueError, AssertionError) as e:
messagebox.showerror("Quantity Error", e)
return
response = requests.post(
API_ENDPOINT + "update_portfolio", payload
).json()
if response.get("error") is not None:
messagebox.showerror("Buy Stock Error", response["error"])
else:
self._master._master.switch_frame(
DashboardFrame, "PyStock | Dashboard", response
)
class RootWindow(ThemedTk):
"""This class provides styled RootWindow (i.e. tk.Tk)"""
def __init__(self, frame, title, theme="yaru", user_data=None):
"""
Constructor for RootWindow
Args:
frame: Frame to be contained in window.
title: Window title.
theme: Overall theme.
user_data: If it is not None, it is supplied in DashboardFrame
Constructor.
Returns:
None
"""
ThemedTk.__init__(self, theme=theme, background=True)
self._frame = None
self._user_data = None
self._theme = theme
if user_data is not None:
self._frame = frame(self, user_data)
self._user_data = user_data
else:
self._frame = frame(self)
self._frame.grid(row=0, column=0)
self.title(title)
self.iconphoto(
False, tk.PhotoImage(file=pathlib.Path("assets/icon.png"))
)
self.resizable(0, 0)
def switch_frame(self, frame, title, user_data=None):
"""
This function destroys current window and opens a new one with given
frame.
Args:
frame: New frame,.
title: Window title.
user_data: If it is not None, it is supplied in DashboardFrame
if new frame is DashboardFrame.
"""
self.destroy()
self.__init__(frame, title, self._theme, user_data)
def change_theme(self, theme):
"""
This function changes the current theme of whole window and it's
children.
Args:
theme: New theme name.
Returns:
None
"""
self._theme = theme
self.set_theme(theme)
def run(self):
"""
This function runs RootWindow by calling it's mainloop.
Args:
None
Returns:
None
"""
self.mainloop()