forked from lhoghu/python-tools
/
gui.py
327 lines (250 loc) · 10.5 KB
/
gui.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
import wx
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
import logging
import datetime
import data_retrieval
import data_loader
import timeseries
################################################################################
class ApplicationMenu(wx.MenuBar):
'''
This is the menu bar for file, edit, help, ... menu items
'''
def __init__(self, parent):
logging.info('Creating menu bar')
# Persist parent state for interaction with the listeners
self.parent = parent
# Add each menu heading in turn
# (just a File menu for now)
wx.MenuBar.__init__(self)
self.Append(self.create_filemenu(self.parent),"&File")
# Add the MenuBar to the Frame content.
parent.Show(True)
def create_filemenu(self, parent):
# Create a file menu
filemenu = wx.Menu()
# wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
menu_about = filemenu.Append(wx.ID_ABOUT, "&About", " Demo")
filemenu.AppendSeparator()
parent.Bind(wx.EVT_MENU, self.on_menu_about, menu_about)
menu_exit = filemenu.Append(wx.ID_EXIT,"E&xit"," Exit the program")
parent.Bind(wx.EVT_MENU, self.on_menu_exit, menu_exit)
return filemenu
#
# Listeners
#
def on_menu_about(self, event):
# A message dialog box with an OK button.
# wx.OK is a standard ID in wxWidgets.
popup(self.parent,
'A simple charting test programme',
self.parent.config.title,
wx.OK)
def on_menu_exit(self, event):
# Close the application
self.parent.Close(True)
################################################################################
class InfoPanel():
def __init__(self, parent, container, config):
logging.info('Creating the info panel')
self.parent = parent
self.header = wx.StaticBox(container, -1, 'Series statistics:')
self.current_label = wx.StaticText(container, -1, 'Current:')
self.min_val_label = wx.StaticText(container, -1, 'Min:')
self.max_val_label = wx.StaticText(container, -1, 'Max:')
self.ave_label = wx.StaticText(container, -1, 'Average:')
self.sd_label = wx.StaticText(container, -1, 'Std dev:')
self.zscore_label = wx.StaticText(container, -1, 'Z-Score:')
self.current = wx.StaticText(container)
self.min_val = wx.StaticText(container)
self.max_val = wx.StaticText(container)
self.ave = wx.StaticText(container)
self.sd = wx.StaticText(container)
self.zscore = wx.StaticText(container)
def layout(self):
sizer = wx.StaticBoxSizer(self.header, wx.VERTICAL)
flags = wx.ALIGN_LEFT
sizer.AddSpacer(self.parent.GetFont().GetPointSize())
stats_grid = wx.FlexGridSizer(cols=2, hgap=5)
stats_grid.Add(self.current_label, flag=flags)
stats_grid.Add(self.current, flag=wx.EXPAND)
stats_grid.Add(self.min_val_label, flag=flags)
stats_grid.Add(self.min_val, flag=wx.EXPAND)
stats_grid.Add(self.max_val_label, flag=flags)
stats_grid.Add(self.max_val, flag=wx.EXPAND)
stats_grid.Add(self.ave_label, flag=flags)
stats_grid.Add(self.ave, flag=wx.EXPAND)
stats_grid.Add(self.sd_label, flag=flags)
stats_grid.Add(self.sd, flag=wx.EXPAND)
stats_grid.Add(self.zscore_label, flag=flags)
stats_grid.Add(self.zscore, flag=wx.EXPAND)
stats_grid.AddGrowableCol(1, 1)
sizer.Add(stats_grid, flag=flags)
return sizer
def update(self, data, config):
ts = sorted(zip(data['dates'], data['values']))
self.current.SetLabel('{0}'.format(ts[-1][1]))
min = timeseries.min(ts)[0]
self.min_val.SetLabel('{0} ({1})'.
format(min[1], min[0].strftime('%Y-%m-%d')))
max = timeseries.max(ts)[0]
self.max_val.SetLabel('{0} ({1})'.
format(max[1], max[0].strftime('%Y-%m-%d')))
self.ave.SetLabel('{0:.2f}'.format(timeseries.mean(ts)[0][1]))
self.sd.SetLabel('{0:.2f}'.format(timeseries.sd(ts)[0][1]))
self.zscore.SetLabel('{0:.2f}'.
format(timeseries.zscore(ts)[0][1]))
################################################################################
class ControlPanel():
def __init__(self, parent, container, config):
logging.info('Creating the control panel')
# wx.Panel.__init__(self, parent)
self.parent = parent
# Text box that allows the user to select the series to view
self.symbol_label= wx.StaticText(container, -1, 'Symbol')
self.symbol_textbox = wx.TextCtrl(
container, style=wx.TE_PROCESS_ENTER)
self.parent.Bind(
wx.EVT_TEXT_ENTER,
self.on_select_symbol,
self.symbol_textbox)
self.symbol_textbox.SetValue(str(config.symbol))
# Create a button that allows the user to create a new
# data set
self.redraw_button = wx.Button(container, -1, 'Redraw')
self.parent.Bind(
wx.EVT_BUTTON,
self.on_redraw_button,
self.redraw_button)
def layout(self):
'''
Return a sizer that lays out the control panel elements
'''
sizer = wx.BoxSizer(wx.HORIZONTAL)
flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL
sizer.Add(self.symbol_label, flag=flags)
sizer.Add(self.symbol_textbox , 0, border=3, flag=flags)
sizer.Add(self.redraw_button, 0, border=3, flag=wx.ALIGN_RIGHT)
return sizer
#
# Listeners
#
def on_select_symbol(self, event):
'''
Called when the listener fires on the set sample size text box
'''
self.parent.config.symbol = self.symbol_textbox.GetValue()
logging.debug('Redrawing chart for symbol ' +
self.parent.config.symbol)
data = get_data(self.parent.config.date, self.parent.config.symbol)
self.parent.chart.draw(data, self.parent.config)
self.parent.chart_info.update(data, self.parent.config)
def on_redraw_button(self, event):
'''
Called when the listener fires on the redraw button
'''
logging.debug('User clicked the redraw button')
self.on_select_symbol(event)
################################################################################
def popup(parent, message, caption, style):
'''
Wrap the dialogue box popup into a single function call
'''
dlg = wx.MessageDialog(parent, message, caption, style)
dlg.ShowModal()
dlg.Destroy()
def get_data(date, symbol):
args = {
'symbol': symbol,
'start': datetime.datetime(2003, 11, 11),
'end': datetime.datetime(2013, 11, 11)
}
loader = 'download_yahoo_timeseries'
ts = data_retrieval.get_time_series(loader, args)
dates, values = zip(*ts[args['symbol']][data_loader.TIMESERIES])
return { 'dates': dates, 'values': [float(v) for v in values] }
################################################################################
class ChartCanvas():
'''
Container for the matplotlib (or any other) chart object
'''
def __init__(self, container, config):
# Create the matplotlib figure and attach it to a canvas
self.figure = Figure(
(config.chart_width, config.chart_height),
dpi=config.chart_dpi)
self.canvas = FigureCanvasWxAgg(container, -1, self.figure)
self.chart = self.figure.add_subplot(111)
def layout(self):
return self.canvas
def draw(self, data, config):
'''
Redraw figure
'''
logging.debug('Redrawing time series')
self.chart.clear()
# self.axes.grid(self.cb_grid.IsChecked())
self.chart.plot(data['dates'], data['values'])
self.figure.autofmt_xdate()
self.canvas.draw()
################################################################################
class ApplicationConfig():
'''
Constant config that's shared between the application elements
'''
title = 'python-tools'
chart_dpi = 100
chart_height = 4.0
chart_width = 5.0
symbol = 'IBM'
date = datetime.datetime(2011, 11, 11)
################################################################################
class ApplicationFrame(wx.Frame):
'''
This is the primary application interface.
It contains panels the user can interact with (menu bar, control
panel, charting window, ...)
'''
def __init__(self):
self.config = ApplicationConfig()
wx.Frame.__init__(self, None, -1, self.config.title)
# Add the menu bar
menu = ApplicationMenu(self)
self.SetMenuBar(menu)
# Create the panel that contains the chart
self.panel = wx.Panel(self)
# self.chart_panel = wx.Panel(self)
self.chart = ChartCanvas(self.panel, self.config)
self.chart_info = InfoPanel(self, self.panel, self.config)
chart_sizer = wx.BoxSizer(wx.HORIZONTAL)
chart_sizer.Add(self.chart.layout(), 3, wx.EXPAND | wx.ALL)
chart_sizer.Add(self.chart_info.layout(), 1, wx.EXPAND | wx.ALL, border=5)
# self.chart_panel.SetSizerAndFit(chart_sizer)
# Create a panel for the user input
self.control_panel = ControlPanel(self, self.panel, self.config)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.control_panel.layout(), 0, wx.ALIGN_CENTER | wx.TOP)
sizer.Add(chart_sizer, 1, wx.EXPAND | wx.ALL)
self.panel.SetSizerAndFit(sizer)
# self.CreateStatusBar()
# Draw an initial plot on startup
data = get_data(self.config.date, self.config.symbol)
self.chart.draw(data, self.config)
self.chart_info.update(data, self.config)
################################################################################
def python_tools():
logging.info('Starting python-tools')
app = wx.PySimpleApp()
app.frame = ApplicationFrame()
app.frame.Fit()
app.frame.Center()
app.frame.Show()
app.MainLoop()
################################################################################
if __name__ == '__main__':
logging.basicConfig(level='DEBUG')
python_tools()
################################################################################