Skip to content

Commit 1f3d7bd

Browse files
author
armand
committed
Made hover effect
1 parent 244382c commit 1f3d7bd

1 file changed

Lines changed: 35 additions & 14 deletions

File tree

mprof.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ def read_mprofile_file(filename):
332332
values = value.split(' ')
333333
f_name, mem_start, start, mem_end, end = values[:5]
334334
ts = func_ts.get(f_name, [])
335-
print(f_name, mem_start, start, mem_end, end)
336335
to_append = [float(start), float(end), float(mem_start), float(mem_end)]
337336
if len(values) >= 6:
338337
# There is a stack level field
@@ -458,6 +457,12 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
458457
return mprofile
459458

460459

460+
461+
FLAME_PLOTTER_VARS = {
462+
'hovered_rect': None,
463+
'alpha': None
464+
}
465+
461466
def flame_plotter(filename, index=0, timestamps=True, children=True, options=None):
462467
try:
463468
import pylab as pl
@@ -565,32 +570,48 @@ def level_to_saturation(level):
565570
x0 -= global_start
566571
x1 -= global_start
567572
color = next(colors[y0])
568-
_rect, text = add_timestamp_rectangle(
573+
rect, text = add_timestamp_rectangle(
569574
timestamp_ax,
570575
x0, x1, y0, y1, f,
571576
color=color
572577
)
573-
rectangles[(x0, y0, x1, y1)] = (f, text)
578+
rectangles[(x0, y0, x1, y1)] = (f, text, rect)
574579
func_num += 1
575580

576581
def mouse_motion_handler(event):
582+
print(FLAME_PLOTTER_VARS['hovered_rect'])
577583
x, y = event.xdata, event.ydata
578-
if x is None or y is None:
579-
return
580-
581-
for rect, func_name in rectangles.items():
582-
x0, y0, x1, y1 = rect
583-
if x0 < x < x1 and y0 < y < y1:
584-
# pl.draw()
585-
return
584+
if x is not None and y is not None:
585+
for coord, (name, text, rect) in rectangles.items():
586+
x0, y0, x1, y1 = coord
587+
if x0 < x < x1 and y0 < y < y1:
588+
if FLAME_PLOTTER_VARS['hovered_rect'] == rect:
589+
return
590+
591+
if FLAME_PLOTTER_VARS['hovered_rect'] is not None:
592+
FLAME_PLOTTER_VARS['hovered_rect'].set_alpha(FLAME_PLOTTER_VARS['alpha'])
593+
FLAME_PLOTTER_VARS['hovered_rect'].set_linewidth(1)
594+
595+
FLAME_PLOTTER_VARS['hovered_rect'] = rect
596+
FLAME_PLOTTER_VARS['alpha'] = rect.get_alpha()
597+
FLAME_PLOTTER_VARS['hovered_rect'].set_alpha(0.8)
598+
FLAME_PLOTTER_VARS['hovered_rect'].set_linewidth(3)
599+
pl.draw()
600+
return
601+
602+
if FLAME_PLOTTER_VARS['hovered_rect'] is not None:
603+
FLAME_PLOTTER_VARS['hovered_rect'].set_alpha(FLAME_PLOTTER_VARS['alpha'])
604+
FLAME_PLOTTER_VARS['hovered_rect'].set_linewidth(1)
605+
pl.draw()
606+
FLAME_PLOTTER_VARS['hovered_rect'] = None
586607

587608
def mouse_click_handler(event):
588609
x, y = event.xdata, event.ydata
589610
if x is None or y is None:
590611
return
591612

592-
for rect, func_name in rectangles.items():
593-
x0, y0, x1, y1 = rect
613+
for coord, _ in rectangles.items():
614+
x0, y0, x1, y1 = coord
594615
if x0 < x < x1 and y0 < y < y1:
595616
toolbar = pl.gcf().canvas.toolbar
596617
toolbar.push_current()
@@ -600,7 +621,7 @@ def mouse_click_handler(event):
600621
return
601622

602623
pl.gcf().canvas.mpl_connect('motion_notify_event', mouse_motion_handler)
603-
pl.gcf().canvas.mpl_connect('button_press_event', mouse_click_handler)
624+
pl.gcf().canvas.mpl_connect('button_release_event', mouse_click_handler)
604625

605626
if timestamps:
606627
pl.hlines(max_mem,

0 commit comments

Comments
 (0)