flimkit-bridge 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ import atexit
2
+ import secrets
3
+ import threading
4
+
5
+ from flimkit.plugins import (
6
+ export_rois_geojson,
7
+ get_current_images,
8
+ import_rois_geojson,
9
+ panel_button,
10
+ plugin_config,
11
+ startup,
12
+ tool,
13
+ )
14
+
15
+ from flimkit_bridge import discovery
16
+ from flimkit_bridge.state import build_state
17
+ from flimkit_bridge.server import bind
18
+
19
+ FLIMKIT_PLUGIN_API = 1
20
+
21
+ PLUGIN_NAME = 'flimkit_bridge'
22
+
23
+ DEFAULT_PORT = 8765
24
+
25
+ _state = {
26
+ 'server': None,
27
+ 'thread': None,
28
+ 'token': '',
29
+ 'url': '',
30
+ 'error': '',
31
+ }
32
+
33
+
34
+ def bridge_status():
35
+ return dict(_state)
36
+
37
+
38
+ def _live_state(app):
39
+ state = build_state()
40
+ state.app = app
41
+ return state
42
+
43
+
44
+ def _start_server(app, port=DEFAULT_PORT):
45
+ token = secrets.token_urlsafe(24)
46
+ state = _live_state(app)
47
+ server = bind('127.0.0.1', port, token, state, live=True)
48
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
49
+ thread.start()
50
+ host, bound = server.server_address
51
+ _state.update({
52
+ 'server': server,
53
+ 'thread': thread,
54
+ 'token': token,
55
+ 'url': f'http://{host}:{bound}',
56
+ 'error': '',
57
+ })
58
+ discovery.write(_state['url'], token)
59
+ atexit.register(discovery.remove)
60
+ return _state
61
+
62
+
63
+ @startup('flimkit_bridge_server', order=200)
64
+ def start_bridge(app):
65
+ if _state['server'] is not None:
66
+ return
67
+ cfg = plugin_config(PLUGIN_NAME)
68
+ port = int(cfg.get('port', DEFAULT_PORT) or DEFAULT_PORT)
69
+ try:
70
+ _start_server(app, port)
71
+ except OSError as exc:
72
+ _state['error'] = str(exc)
73
+ print(f'[FLIMKit bridge] could not bind a port: {exc}')
74
+ return
75
+ if _state['url'] != f'http://127.0.0.1:{port}':
76
+ print(f'[FLIMKit bridge] port {port} was busy, using {_state["url"]}')
77
+ print(f'[FLIMKit bridge] listening on {_state["url"]}')
78
+ print(f'[FLIMKit bridge] details written to {discovery.discovery_path()}')
79
+
80
+
81
+ @panel_button('flimkit_bridge_send', 'Send to a viewer', panel='roi', order=200)
82
+ def send_to_clients(app):
83
+ from tkinter import messagebox
84
+ server = _state['server']
85
+ if server is None:
86
+ messagebox.showerror(
87
+ 'FLIMKit bridge',
88
+ 'The bridge is not running.\n\n'
89
+ + (_state['error'] or 'It failed to start with FLIMKit.'),
90
+ parent=getattr(app, 'root', None),
91
+ )
92
+ return
93
+ if not server.state.connected:
94
+ messagebox.showerror(
95
+ 'No viewer is connected',
96
+ 'Nothing has connected to FLIMKit yet.\n\n'
97
+ 'Start QuPath, then choose\n'
98
+ 'Extensions > FLIMKit bridge > Connect...\n\n'
99
+ 'It finds the address and token itself, from\n'
100
+ f'{discovery.discovery_path()}',
101
+ parent=getattr(app, 'root', None),
102
+ )
103
+ return
104
+ images = get_current_images(app)
105
+ names = sorted(images.get('images', {}))
106
+ if not names:
107
+ messagebox.showerror(
108
+ 'FLIMKit bridge',
109
+ 'There is nothing to send yet. Fit a dataset first.',
110
+ parent=getattr(app, 'root', None),
111
+ )
112
+ return
113
+ messagebox.showinfo(
114
+ 'FLIMKit bridge',
115
+ 'FLIMKit is serving ' + ', '.join(names) + '.\n\n'
116
+ 'In QuPath choose\n'
117
+ 'Extensions > FLIMKit bridge > Add FLIMKit images to project',
118
+ parent=getattr(app, 'root', None),
119
+ )
120
+
121
+
122
+ @tool(id='flimkit_bridge_open', label='FLIMKit Bridge...', menu='Tools', order=510)
123
+ def open_bridge(app):
124
+ from tkinter import messagebox
125
+ if _state['server'] is None:
126
+ messagebox.showerror(
127
+ 'FLIMKit bridge',
128
+ 'The bridge is not running.\n\n'
129
+ + (_state['error'] or 'It failed to start with FLIMKit.'),
130
+ parent=getattr(app, 'root', None),
131
+ )
132
+ return
133
+ connected = 'yes' if _state['server'].state.connected else 'no'
134
+ messagebox.showinfo(
135
+ 'FLIMKit bridge',
136
+ f'Address: {_state["url"]}\n'
137
+ f'A viewer is connected: {connected}\n\n'
138
+ f'Pairing details: {discovery.discovery_path()}\n\n'
139
+ 'In QuPath choose\n'
140
+ 'Extensions > FLIMKit bridge > Connect...',
141
+ parent=getattr(app, 'root', None),
142
+ )