Skip to content

Commit df3a0cc

Browse files
committed
The plotting is now asynchronous with the main thread as well.
1 parent d8dd2ab commit df3a0cc

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

floppy/reportWidget.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def _update(self):
9292
# url = QtCore.QUrl.fromLocalFile(self.fileBase)
9393
url = QtCore.QUrl.fromLocalFile(QtCore.QDir(self.fileBase).absoluteFilePath('dummy.html'))
9494
QtWebKit.QWebSettings.clearMemoryCaches()
95-
self.setHtml(tmplt(data, self.cache[:], self.fileBase), url)
95+
html = tmplt(data, self.cache[:], self.fileBase)
96+
if html:
97+
self.setHtml(html, url)
9698

9799

98100
@template

floppy/templates.py

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
import matplotlib.pyplot as plt
33
from floppy.reportWidget import template
44
from numpy import array, arange
5+
import threading
6+
import time
57
import os
68

79
_pointCache = None
10+
plotting = False
811

912
@template
1013
def plotTemplate(data, cache, fileBase):
@@ -117,34 +120,72 @@ def programTemplate(data, cache, fileBase):
117120
name, varType, value in data['outputs']]),
118121
stdout=data['stdout'].replace('\\n', '<br>'))
119122

123+
124+
def plotBars(points, cache, fileName):
125+
globals()['_pointCache'] = cache
126+
# thisPlot = time.time()
127+
# try:
128+
# if thisPlot - lastPlot < 5:
129+
# return
130+
# except:
131+
# lastPlot = thisPlot
132+
# return
133+
# lastPlot = thisPlot
134+
x, y = zip(*points)
135+
n_groups = len(x)
136+
fig, ax = plt.subplots()
137+
index = arange(n_groups)
138+
bar_width = 0.35
139+
opacity = .4
140+
rects1 = plt.bar(index, x, bar_width,
141+
alpha=opacity,
142+
color='b',
143+
label='A')
144+
rects2 = plt.bar(index + bar_width, y, bar_width,
145+
alpha=opacity,
146+
color='r',
147+
label='B')
148+
plt.xticks(index+bar_width)
149+
plt.legend()
150+
plt.tight_layout()
151+
savefig(fileName)
152+
global plotting
153+
plotting = False
154+
120155
@template
121156
def plotBarsGroupedTemplate(data, cache, fileBase):
122157
points = data['points']
123158
fileName = fileBase+'/_ppy_{}.svg'.format(data['ID'])
124159
#fileName = os.path.join(fileBase, '_ppy_{}.svg'.format(data['ID']))
125160
# fileName = '_ppy_{}.svg'.format(data['ID'])
126161
# print(fileName)
127-
162+
global plotting
128163
if not points == globals()['_pointCache'] and points:
129-
x, y = zip(*points)
130-
n_groups = len(x)
131-
fig, ax = plt.subplots()
132-
index = arange(n_groups)
133-
bar_width = 0.35
134-
opacity = .4
135-
rects1 = plt.bar(index, x, bar_width,
136-
alpha=opacity,
137-
color='b',
138-
label='A')
139-
rects2 = plt.bar(index + bar_width, y, bar_width,
140-
alpha=opacity,
141-
color='r',
142-
label='B')
143-
plt.xticks(index+bar_width)
144-
plt.legend()
145-
plt.tight_layout()
146-
savefig(fileName)
147-
globals()['_pointCache'] = cache[:]
164+
if not plotting:
165+
plotting = True
166+
threading.Thread(target=plotBars, args=(points, cache, fileName)).start()
167+
else:
168+
return None
169+
# x, y = zip(*points)
170+
# n_groups = len(x)
171+
# fig, ax = plt.subplots()
172+
# index = arange(n_groups)
173+
# bar_width = 0.35
174+
# opacity = .4
175+
# rects1 = plt.bar(index, x, bar_width,
176+
# alpha=opacity,
177+
# color='b',
178+
# label='A')
179+
# rects2 = plt.bar(index + bar_width, y, bar_width,
180+
# alpha=opacity,
181+
# color='r',
182+
# label='B')
183+
# plt.xticks(index+bar_width)
184+
# plt.legend()
185+
# plt.tight_layout()
186+
# savefig(fileName)
187+
# globals()['_pointCache'] = cache[:]
188+
148189
fileName = '_ppy_{}.svg'.format(data['ID'])
149190
return """<h1 id="head">{nodeName} -- {nodeID}</h1>
150191
<style>

0 commit comments

Comments
 (0)