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

Improve non-parallel demo #40

Merged
merged 16 commits into from
Mar 11, 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
50 changes: 32 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,50 @@ This is useful if you want to use `tqdm` to track the progress of a long-running
```bash
pip install tqdm_publisher
```
## Getting Started
### Basic Usage
To monitor the progress of an existing `tqdm` progress bar, simply swap the `tqdm`and `TQDMPublisher` constructors. Then, declare a callback function to handle progress updates, and subscribe it to the `TQDMPublisher` updates using the `subscribe` method _before iteration begins_.

## Usage
#### Original Code
```python
import random
import asyncio
import time

from tqdm_publisher import TQDMPublisher
from tqdm import tqdm

N_TASKS = 100

# Create a list of tasks
durations = [ random.uniform(0, 1.0) for _ in range(N_TASKS) ]

# Create a progress bar
progress_bar = tqdm(durations)

async def sleep_func(sleep_duration = 1):
await asyncio.sleep(delay=sleep_duration)
# Iterate over the progress bar
for duration in progress_bar:
time.sleep(duration) # Execute the task
```

async def run_multiple_sleeps(sleep_durations):
#### Modified Code

tasks = []
```python
import random
import time

for sleep_duration in sleep_durations:
task = asyncio.create_task(sleep_func(sleep_duration=sleep_duration))
tasks.append(task)
from tqdm_publisher import TQDMPublisher

progress_bar = TQDMPublisher(asyncio.as_completed(tasks), total=len(tasks))
callback_id = progress_bar.subscribe(lambda info: print('Progress Update', info))
N_TASKS = 100
durations = [ random.uniform(0, 1.0) for _ in range(N_TASKS) ]
progress_bar = TQDMPublisher(durations)

for f in progress_bar:
await f
# Declare a callback function to handle progress updates
on_update = lambda info: print('Progress Update', info)

progress_bar.unsubscribe(callback_id)
# Subscribe the callback to the TQDMPublisher
progress_bar.subscribe(on_update)

number_of_tasks = 10**5
sleep_durations = [random.uniform(0, 5.0) for _ in range(number_of_tasks)]
asyncio.run(run_multiple_sleeps(sleep_durations=sleep_durations))
for duration in progress_bar:
time.sleep(duration)
```

## Demo
Expand Down
12 changes: 0 additions & 12 deletions demo/client.py

This file was deleted.

41 changes: 0 additions & 41 deletions demo/demo_cli.py

This file was deleted.

152 changes: 0 additions & 152 deletions demo/server.py

This file was deleted.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ classifiers = [
"Operating System :: MacOS",
"Operating System :: Unix",
]

dependencies = [
"tqdm>=4.49.0"
]
Expand All @@ -50,8 +51,8 @@ demo = [
"Homepage" = "https://github.com/catalystneuro/tqdm_publisher"
"Bug Tracker" = "https://github.com/catalystneuro/tqdm_publisher/issues"

[project.gui-scripts]
tqdm_publisher = "demo.demo_cli:main"
[project.scripts]
tqdm_publisher = "tqdm_publisher._demo._demo_command_line_interface:_command_line_interface"

[tool.black]
line-length = 120
Expand Down
Empty file.
49 changes: 7 additions & 42 deletions demo/client.html → src/tqdm_publisher/_demo/_client.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<title>Concurrent Client Demo</title>

<!-- Basic Page Styling -->
<style>

html, body {
Expand Down Expand Up @@ -45,66 +46,30 @@
background-color: #ddd;
}



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

</style>
<script src="./_client.js" defer></script>

</head>

<!-- Basic Page Structure -->
<body>
<header>
<div>
<h1>tqdm_progress</h1>
<i><small>Create multiple progress bars to test concurrent subscriptions</small></i>
</div>

<!-- Declare a button to create progress bars -->
<button>Create Progress Bar</button>
</header>

<div id="bars">

</div>

<!-- Declare a container to hold the progress bars -->
<div id="bars"></div>
</body>

<script>

const bars = document.querySelector('#bars');

class ProgressClient {
constructor() {
this.socket = new WebSocket('ws://localhost:8000');

this.element = document.createElement('div');
this.element.classList.add('progress');
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() {
this.socket.close();
this.element.remove()
}

}

const button = document.querySelector('button');
button.addEventListener('click', () => {
const client = new ProgressClient();
})

</script>

</html>
Loading
Loading