-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathipython_anybar.py
More file actions
54 lines (40 loc) · 1.42 KB
/
ipython_anybar.py
File metadata and controls
54 lines (40 loc) · 1.42 KB
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
from anybar import AnyBar
from IPython.core.magic import register_line_magic
class LineWatcher(object):
def __init__(self):
self.start_flag = False
self.anybar = None
def connect(self, host='localhost', port='1738'):
if self.anybar:
self.anybar.change('green')
self.anybar = AnyBar(port=int(port), address=host)
print('Connect to {host}:{port}'.format(host=host, port=int(port)))
def start(self):
self.start_flag = True
if self.anybar:
self.anybar.change('red')
def stop(self):
if self.start_flag:
self.start_flag = False
if self.anybar:
self.anybar.change('green')
antbar_watcher = None
def load_ipython_extension(ip):
global antbar_watcher
antbar_watcher = LineWatcher()
ip.events.register('pre_run_cell', antbar_watcher.start)
ip.events.register('post_run_cell', antbar_watcher.stop)
def unload_ipython_extension(ip):
ip.events.unregister('pre_run_cell', antbar_watcher.start)
ip.events.unregister('post_run_cell', antbar_watcher.stop)
if antbar_watcher:
del antbar_watcher
@register_line_magic
def ipython_anybar_connect(line):
parametrs = line.strip().split()
if len(parametrs) == 2:
antbar_watcher.connect(*parametrs)
elif len(parametrs) == 1:
antbar_watcher.connect(port=parametrs[0])
else:
antbar_watcher.connect()