diff --git a/src/tqdm_publisher/demo/client.html b/src/tqdm_publisher/demo/client.html index 35b9031..3b716e2 100644 --- a/src/tqdm_publisher/demo/client.html +++ b/src/tqdm_publisher/demo/client.html @@ -105,7 +105,7 @@

tqdm_progress

constructor(props) { this.#connect(props); - + } close() { @@ -113,7 +113,7 @@

tqdm_progress

} } - + const bars = {} const client = new ProgressClient({ diff --git a/src/tqdm_publisher/demo/server.py b/src/tqdm_publisher/demo/server.py index 10787f5..abaa212 100644 --- a/src/tqdm_publisher/demo/server.py +++ b/src/tqdm_publisher/demo/server.py @@ -4,18 +4,19 @@ import json import random import threading +import time from typing import List from uuid import uuid4 -import time import websockets from tqdm_publisher import TQDMPublisher -def generate_task_durations(n = 100) -> List[float]: +def generate_task_durations(n=100) -> List[float]: return [random.uniform(0, 1.0) for _ in range(n)] + def start_progress_bar(id, callback): durations = generate_task_durations() progress_bar = TQDMPublisher(durations) @@ -23,12 +24,12 @@ def start_progress_bar(id, callback): for duration in progress_bar: time.sleep(duration) + class WebSocketHandler: def __init__(self): self.clients = {} pass - async def send(self, id, data): await self.clients[id].send(json.dumps(data)) @@ -38,22 +39,19 @@ async def handler(self, websocket): def on_progress(id, info): - asyncio.run(self.send(identifier, dict( - id=id, - payload=info - ))) + asyncio.run(self.send(identifier, dict(id=id, payload=info))) try: async for message in websocket: info = json.loads(message) - if (info["command"] == "start"): + if info["command"] == "start": thread = threading.Thread(target=start_progress_bar, args=[info["id"], on_progress]) thread.start() finally: - del self.clients[identifier] # This is called when the connection is closed + del self.clients[identifier] # This is called when the connection is closed async def spawn_server(): @@ -61,8 +59,10 @@ async def spawn_server(): async with websockets.serve(handler, "", 8000): await asyncio.Future() + def main(): asyncio.run(spawn_server()) + if __name__ == "__main__": main()