forked from C1ickz/Stock-Predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainGUI.py
175 lines (153 loc) · 5.77 KB
/
mainGUI.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
# Developed by: Jordan Davis and Ryan Harris
#
# Emails: davisja2023@mountunion.edu and rh297019@ohio.edu
#
# Description:This program has a user-facing GUI that interacts
# with a server. The server predicts future stock prices using
# an Long-Short-Term-Memory (LSTM) network. The training of
# the model takes place on the server and the result of the
# prediction is sent back to the client with as little latency
# as possible.
#
# NOTE: The server object was also designed and implemented
# by Jordan Davis and Ryan Harris.
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import Button, Label, Entry
from stockclient import StockPSocket
from PIL import Image, ImageTk
class MainGUI:
def __init__(self, master):
"""
Constructor: Creates the GUI and creates a socket
Args:
self: calling object
master: parent widget
Returns:
void
"""
# sets host and port of server
self.host = '10.18.207.18'
self.port = 9998
# sets up empty var for ticker
self.tkr = ''
# sets up prediction variable
self.prediction = 0
self.master = master
master.title('Stock Predictor')
master.geometry('800x600')
# creates label
self.lbl = Label(master, text='Input Stock Ticker')
self.lbl.pack(side=TOP)
# creates text field
self.txt = Entry(master, width=10)
self.txt.pack(side=TOP)
# creates submission button
self.btn = Button(master, text='Submit', command=self.clicked_tkr)
self.btn.pack(side=TOP)
# creates prediction explicit label
self.p_lbl = Label(master, text='Prediction(1 day in the future): ')
self.p_lbl.pack(side=TOP)
# creates prediction output label
self.p_out_lbl = Label(master, text=self.get_prediction())
self.p_out_lbl.config(font=17)
self.p_out_lbl.pack(side=TOP)
def disp_graph(self, wait=False):
"""
Displays the image stored as imgFile.png to the GUI
Args:
self: calling object
wait: boolean True if waiting for image False if not
Returns:
void
"""
try:
if wait:
img = Image.open('waitImg.png')
else:
img = Image.open('imgFile.png')
render = ImageTk.PhotoImage(img)
# create image label
img = Label(self.master, image=render)
img.image = render
img.place(x=95, y=110)
except OSError as OSE:
messagebox.showinfo('Graph Retrieval Failed', 'The graph requested was not received correctly. Please '
'submit the request again.')
def clicked_tkr(self):
"""
Gathers the input from self.txt, verifies it, then requests a prediction and a graph. It then finally calls
disp_graph(). Also handles server offline and invalid input cases.
Args:
self: calling object
Returns:
void
"""
ticker = ""
try:
if self.txt.get().strip():
ticker = self.txt.get().strip()
else:
raise ValueError('Empty string')
self.update_prediction_out('Please wait. This could take up to 1 minute.')
client_socket = StockPSocket(self.host, self.port)
if client_socket.validate(ticker + 'v') == 'error':
client_socket.close()
self.update_prediction_out('Invalid input')
raise ValueError()
else:
self.tkr = ticker
self.disp_graph(wait=True)
# gets predicted value of stock
client_socket = StockPSocket(self.host, self.port)
client_socket.send_request(self.tkr + 'p')
prediction = client_socket.rec_pred()
client_socket.close()
self.update_prediction_out(prediction) # after Ryan finishes lstm prediction
# gets graph of stock
client_socket = StockPSocket(self.host, self.port)
client_socket.send_request(self.tkr + 'g')
client_socket.receive()
client_socket.close()
self.disp_graph()
except ValueError as VE:
messagebox.showinfo('Invalid Ticker', 'Please enter a proper stock ticker. If you are trying to get data '
'about a market index prefix the ticker with a ^ (ie. ^dji).')
except ConnectionRefusedError as CRE:
messagebox.showinfo('Server Offline', 'Sorry for the inconvenience, but this service is not available '
'right now.')
def set_prediction(self, prediction):
"""
Updates the prediction variable
Args:
self: calling object
prediction: int updated value
Returns:
void
"""
self.prediction = prediction
def get_prediction(self):
"""
Returns the current value stored in prediction
Args:
self: calling object
Returns:
prediction: int
"""
return self.prediction
def update_prediction_out(self, prediction):
"""
Updates the prediction label
Args:
self: calling object
prediction: int value for updated presiction
Returns:
void
"""
self.set_prediction(prediction)
self.p_out_lbl['text'] = self.get_prediction()
self.p_out_lbl.update()
root = Tk()
root.resizable(width=False, height=False)
my_gui = MainGUI(root)
root.mainloop()