-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
48 lines (40 loc) · 1.67 KB
/
Copy pathgui.py
File metadata and controls
48 lines (40 loc) · 1.67 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
from bokeh.models import DataSource
from bokeh.plotting import figure
from bokeh.server.server import Server
import asyncio
from tornado.ioloop import IOLoop
from bokeh.document import Document
import websockets
class RandDocument():
doc: Document | None = None
textDataSource: DataSource | None = None
def __init__(self):
io_loop = IOLoop.current()
self.server = Server({'/': self.bkapp}, io_loop=io_loop, port=5006)
self.server.start()
self.server.io_loop.add_callback(self.server.show, "/")
def bkapp(self, doc):
self.doc = doc
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.outline_line_color = None
p.grid.grid_line_color = None
p.xaxis.visible = False
p.yaxis.visible = False
# add a text renderer to the plot (no data yet)
r = p.text(x=[50], y=[50], text=["?"], text_color=["#000000"], text_font_size="64px",
text_baseline="middle", text_align="center")
self.textDataSource = r.data_source
doc.add_root(p)
async def receive_new_numbers(self, websocket):
async for message in websocket:
n = int.from_bytes(message, byteorder='little', signed=False)
print(f"Received number: {n}")
self.doc.add_next_tick_callback(lambda: self.textDataSource.data.update({"text": [str(n)]}))
async def main():
rd = RandDocument()
async with websockets.connect('ws://localhost:9001') as websocket:
print("Connected to server")
asyncio.create_task(rd.receive_new_numbers(websocket))
await asyncio.Future()
if __name__ == '__main__':
asyncio.run(main())