class BaseVis(object): def __init__(self, viz_opts, update_mode='append', env=None, win=None, resume=False, port=8097): self.viz_opts = viz_opts self.update_mode = update_mode self.win = win if env is None: env = 'main' self.viz = Visdom(env=env, port=port) # if resume first plot should not update with replace self.removed = not resume def win_exists(self): return self.viz.win_exists(self.win) def close(self): if self.win is not None: self.viz.close(win=self.win) self.win = None def register_event_handler(self, handler): self.viz.register_event_handler(handler, self.win)
callback_text_window = viz.text(txt) def type_callback(event): if event['event_type'] == 'KeyPress': curr_txt = event['pane_data']['content'] if event['key'] == 'Enter': curr_txt += '<br>' elif event['key'] == 'Backspace': curr_txt = curr_txt[:-1] elif event['key'] == 'Delete': curr_txt = txt elif len(event['key']) == 1: curr_txt += event['key'] viz.text(curr_txt, win=callback_text_window) viz.register_event_handler(type_callback, callback_text_window) # matplotlib demo: try: import matplotlib.pyplot as plt plt.plot([1, 23, 2, 4]) plt.ylabel('some numbers') viz.matplot(plt) except BaseException as err: print('Skipped matplotlib example') print('Error message: ', err) # video demo: try: video = np.empty([256, 250, 250, 3], dtype=np.uint8) for n in range(256):
caption='Press arrows to alter color.'), win=win) image_color = 0 callback_image_window = show_color_image_window(image_color) def image_callback(event): global image_color if event['event_type'] == 'KeyPress': if event['key'] == 'ArrowRight': image_color = min(image_color + 0.2, 1) if event['key'] == 'ArrowLeft': image_color = max(image_color - 0.2, 0) show_color_image_window(image_color, callback_image_window) viz.register_event_handler(image_callback, callback_image_window) # text window with Callbacks txt = 'This is a write demo notepad. Type below. Delete clears text:<br>' callback_text_window = viz.text(txt) def type_callback(event): if event['event_type'] == 'KeyPress': curr_txt = event['pane_data']['content'] if event['key'] == 'Enter': curr_txt += '<br>' elif event['key'] == 'Backspace': curr_txt = curr_txt[:-1] elif event['key'] == 'Delete': curr_txt = txt elif len(event['key']) == 1:
image_win_id = viz.image(image * color, opts=opts, win='win01') # 指定窗口id为 'win01' def image_callback(event): # 回调函数 global color # 全局变量 if event['event_type'] == 'KeyPress': # 处理左/右按键 if event['key'] == 'ArrowRight': color = min(color + 0.1, 1) if event['key'] == 'ArrowLeft': color = max(color - 0.1, 0) viz.image(image * color, opts=opts, win=image_win_id) # 向之前的窗口绘制指定颜色 elif event['event_type'] == 'Click': # 目前 click 事件有bug print(event['image_coord']) viz.register_event_handler(image_callback, image_win_id) # 将回调函数与窗口id绑定 # visdom对象内部保存有一个字典: event_handlers: {'win_id': [func1, func2, ...], ...} # 该函数其实是执行: viz.event_handlers['win_id'].append(callbackfunc) # 相对应的有删除回调函数的接口: clear_event_handler, 作用是 viz.event_handlers['win_id'] = [] """ 2. 具有回调函数的文字窗口示例: 应用事件 KeyPress """ txt = 'This is a write demo notepad. Type below. [Delete]: clears text.<br>' # 创建一个 text 窗口并返回 id text_window_id = viz.text(txt, win='win02', opts={'title': 'text-demo'}) def type_callback(event): # 输入回调函数 if event['event_type'] == 'KeyPress': # 键盘响应 curr_txt = event['pane_data']['content'] # 读取窗口中已输入的内容 if event['key'] == 'Enter': # 根据按键将新内容写入 curr_txt curr_txt += '<br>' # 换行 elif event['key'] == 'Backspace': curr_txt = curr_txt[:-1]
win=win ) image_color = 0 callback_image_window = show_color_image_window(image_color) def image_callback(event): global image_color if event['event_type'] == 'KeyPress': if event['key'] == 'ArrowRight': image_color = min(image_color + 0.2, 1) if event['key'] == 'ArrowLeft': image_color = max(image_color - 0.2, 0) show_color_image_window(image_color, callback_image_window) viz.register_event_handler(image_callback, callback_image_window) # text window with Callbacks txt = 'This is a write demo notepad. Type below. Delete clears text:<br>' callback_text_window = viz.text(txt) def type_callback(event): if event['event_type'] == 'KeyPress': curr_txt = event['pane_data']['content'] if event['key'] == 'Enter': curr_txt += '<br>' elif event['key'] == 'Backspace': curr_txt = curr_txt[:-1] elif event['key'] == 'Delete': curr_txt = txt elif len(event['key']) == 1: