Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger CI; remove minor comments; also run pre-commit #22

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/assess-file-changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ jobs:
else
echo "Changelog not updated"
fi
done
done
2 changes: 1 addition & 1 deletion .github/workflows/auto-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
with:
verbose: true
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
password: ${{ secrets.PYPI_API_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Codespell
uses: codespell-project/actions-codespell@v1
uses: codespell-project/actions-codespell@v1
2 changes: 1 addition & 1 deletion .github/workflows/dailies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
subject: TQDM Publisher Daily Test Failure
to: [email protected],[email protected] # add more with commas, no separation
from: TQDM Publisher
body: "The daily workflow for TQDM Publisher failed: please check status at https://github.com/CatalystNeuro/tqdm_publisher/actions/workflows/dailies.yml"
body: "The daily workflow for TQDM Publisher failed: please check status at https://github.com/CatalystNeuro/tqdm_publisher/actions/workflows/dailies.yml"
2 changes: 1 addition & 1 deletion .github/workflows/deploy-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
uses: re-actors/alls-green@release/v1
with:
allowed-skips: run-tests # Sometimes only docs are adjusted
jobs: ${{ toJSON(needs) }}
jobs: ${{ toJSON(needs) }}
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

- name: Run full pytest with coverage
run: pytest -rsx -n auto --dist loadscope --cov=./ --cov-report xml:./codecov.xml

- name: Upload full coverage to Codecov
if: ${{ matrix.python-version == '3.9' && matrix.os == 'ubuntu-latest' }}
uses: codecov/codecov-action@v3
Expand All @@ -38,4 +38,4 @@ jobs:
file: ./codecov.xml
flags: unittests
name: codecov-umbrella
yml: ./codecov.yml
yml: ./codecov.yml
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dist

.coverage
.coverage.*
codecov.xml
codecov.xml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def sleep_func(sleep_duration = 1):
await asyncio.sleep(delay=sleep_duration)

async def run_multiple_sleeps(sleep_durations):

tasks = []

for sleep_duration in sleep_durations:
Expand Down
36 changes: 18 additions & 18 deletions demo/client.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Concurrent Client Demo</title>

<style>
Expand Down Expand Up @@ -44,19 +44,19 @@
height: 20px;
background-color: #ddd;
}



.progress div {
height: 100%;
background-color: #4caf50;
width: 0%;
}

</style>

</head>

<body>
<header>
<div>
Expand All @@ -69,9 +69,9 @@ <h1>tqdm_progress</h1>
<div id="bars">

</div>

</body>

<script>

const bars = document.querySelector('#bars');
Expand All @@ -85,12 +85,12 @@ <h1>tqdm_progress</h1>
const progress = document.createElement('div');
this.element.appendChild(progress);
bars.appendChild(this.element);

this.socket.addEventListener('message', function (event) {
const data = JSON.parse(event.data);
progress.style.width = 100 * (data.n / data.total) + '%';
});

}

close() {
Expand All @@ -104,7 +104,7 @@ <h1>tqdm_progress</h1>
button.addEventListener('click', () => {
const client = new ProgressClient();
})

</script>
</html>

</html>
5 changes: 3 additions & 2 deletions demo/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import subprocess

from pathlib import Path

client_path = Path(__file__).parent / "client.html"


def main():
subprocess.run(["open", client_path])


if __name__ == "__main__":
main()
main()
19 changes: 9 additions & 10 deletions demo/demo_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
demo_base_path = Path(__file__).parent

client_path = demo_base_path / "client.html"
server_path = demo_base_path/ "server.py"
server_path = demo_base_path / "server.py"

def main():

if (len(sys.argv) <= 1):
def main():
if len(sys.argv) <= 1:
print("No command provided. Please specify a command (e.g. 'demo').")
return

command = sys.argv[1]

flags_list = sys.argv[2:]
Expand All @@ -22,15 +22,14 @@ def main():
both_flags = "--server" in flags_list and "--client" in flags_list

flags = dict(
client = not server_flag or both_flags,
server = not client_flag or both_flags,

client=not server_flag or both_flags,
server=not client_flag or both_flags,
)

if (command == "demo"):
if command == "demo":
if flags["client"]:
subprocess.run(["open", client_path])

if flags["server"]:
subprocess.run(["python", server_path])

Expand All @@ -39,4 +38,4 @@ def main():


if __name__ == "__main__":
main()
main()
58 changes: 25 additions & 33 deletions demo/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env python

import random
import asyncio
import json
import random
import threading
from typing import List

from tqdm_publisher import TQDMPublisher
from uuid import uuid4

import websockets
import threading
from uuid import uuid4

import json
from tqdm_publisher import TQDMPublisher


async def sleep_func(sleep_duration: float = 1) -> float:
await asyncio.sleep(delay=sleep_duration)
Expand All @@ -28,31 +28,28 @@ def create_tasks():
return tasks


class ProgressHandler():

class ProgressHandler:
def __init__(self):
self.started = False
self.callbacks = []
self.callback_ids = []

def subscribe(self, callback):
def subscribe(self, callback):
self.callbacks.append(callback)

if (hasattr(self, 'progress_bar')):
if hasattr(self, "progress_bar"):
self._subscribe(callback)


def unsubscribe(self, callback_id):
self.progress_bar.unsubscribe(callback_id)

def clear(self):
self.callbacks = []
def clear(self):
self.callbacks = []
self._clear()

def _clear(self):

for callback_id in self.callback_ids:
self.unsubscribe(callback_id)
self.unsubscribe(callback_id)

self.callback_ids = []

Expand All @@ -65,15 +62,12 @@ def stop(self):
self.clear()
self.thread.join()


def _subscribe(self, callback):
callback_id = self.progress_bar.subscribe(callback)
self.callback_ids.append(callback_id)


async def run(self):

if (hasattr(self, 'progress_bar')):
if hasattr(self, "progress_bar"):
print("Progress bar already running")
return

Expand All @@ -82,35 +76,32 @@ async def run(self):

for callback in self.callbacks:
self._subscribe(callback)

for f in self.progress_bar:
await f

self._clear()
del self.progress_bar



def thread_loop(self):
while self.started:
asyncio.run(self.run())

def start(self):

if (self.started):
def start(self):
if self.started:
return

self.started = True

self.thread = threading.Thread(target=self.thread_loop) # Start infinite loop of progress bar thread
self.thread = threading.Thread(target=self.thread_loop) # Start infinite loop of progress bar thread
self.thread.start()


progress_handler = ProgressHandler()


class WebSocketHandler:
def __init__(self):

self.clients = {}

# Initialize with any state you need
Expand All @@ -126,9 +117,9 @@ def handle_task_result(self, task):

async def handler(self, websocket):
id = str(uuid4())
self.clients[id] = websocket # Register client connection
self.clients[id] = websocket # Register client connection

progress_handler.start() # Start if not started
progress_handler.start() # Start if not started

def on_progress(info):
task = asyncio.create_task(websocket.send(json.dumps(info)))
Expand All @@ -143,18 +134,19 @@ def on_progress(info):
finally:
# This is called when the connection is closed
del self.clients[id]
if (len(self.clients) == 0):
if len(self.clients) == 0:
progress_handler.stop()



async def spawn_server():
handler = WebSocketHandler().handler
async with websockets.serve(handler, "", 8000):
await asyncio.Future() # run forever


def main():
asyncio.run(spawn_server())


if __name__ == "__main__":
main()
main()
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ dependencies = [

[project.optional-dependencies]
test = [
"pytest",
"pytest-asyncio",
"pytest",
"pytest-asyncio",
"pytest-cov"
]

Expand Down
2 changes: 1 addition & 1 deletion src/tqdm_publisher/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .publisher import TQDMPublisher
from .publisher import TQDMPublisher
Loading
Loading