• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

celery_progress/H05-Jul-2021-313242

celery_progress.egg-info/H03-May-2022-224168

LICENSEH A D05-Jul-20211 KiB2217

MANIFEST.inH A D05-Jul-2021102 54

PKG-INFOH A D05-Jul-20218.1 KiB224168

README.mdH A D05-Jul-20217 KiB192139

setup.cfgH A D05-Jul-202138 53

setup.pyH A D05-Jul-20211.7 KiB5245

README.md

1# Celery Progress Bars for Django
2
3Drop in, dependency-free progress bars for your Django/Celery applications.
4
5Super simple setup. Lots of customization available.
6
7## Demo
8
9[Celery Progress Bar demo on Build With Django](https://buildwithdjango.com/projects/celery-progress/)
10
11### Github demo application: build a download progress bar for Django
12Starting with Celery can be challenging, [eeintech](https://github.com/eeintech) built a complete [Django demo application](https://github.com/eeintech/django-celery-progress-demo) along with a [step-by-step guide](https://eeinte.ch/stream/progress-bar-django-using-celery/) to get you started on building your own progress bar!
13
14## Installation
15
16If you haven't already, make sure you have properly [set up celery in your project](https://docs.celeryproject.org/en/stable/getting-started/first-steps-with-celery.html#first-steps).
17
18Then install this library:
19
20```bash
21pip install celery-progress
22```
23
24## Usage
25
26### Prerequisites
27
28First add `celery_progress` to your `INSTALLED_APPS` in `settings.py`.
29
30Then add the following url config to your main `urls.py`:
31
32```python
33from django.urls import re_path, include
34re_path(r'^celery-progress/', include('celery_progress.urls')),  # the endpoint is configurable
35```
36
37### Recording Progress
38
39In your task you should add something like this:
40
41```python
42from celery import shared_task
43from celery_progress.backend import ProgressRecorder
44import time
45
46@shared_task(bind=True)
47def my_task(self, seconds):
48    progress_recorder = ProgressRecorder(self)
49    result = 0
50    for i in range(seconds):
51        time.sleep(1)
52        result += i
53        progress_recorder.set_progress(i + 1, seconds)
54    return result
55```
56
57You can add an optional progress description like this:
58
59```python
60  progress_recorder.set_progress(i + 1, seconds, description='my progress description')
61```
62
63### Displaying progress
64
65In the view where you call the task you need to get the task ID like so:
66
67**views.py**
68```python
69def progress_view(request):
70    result = my_task.delay(10)
71    return render(request, 'display_progress.html', context={'task_id': result.task_id})
72```
73
74Then in the page you want to show the progress bar you just do the following.
75
76#### Add the following HTML wherever you want your progress bar to appear:
77
78**display_progress.html**
79```html
80<div class='progress-wrapper'>
81  <div id='progress-bar' class='progress-bar' style="background-color: #68a9ef; width: 0%;">&nbsp;</div>
82</div>
83<div id="progress-bar-message">Waiting for progress to start...</div>
84```
85
86#### Import the javascript file.
87
88**display_progress.html**
89```html
90<script src="{% static 'celery_progress/celery_progress.js' %}"></script>
91```
92
93#### Initialize the progress bar:
94
95```javascript
96// vanilla JS version
97document.addEventListener("DOMContentLoaded", function () {
98  var progressUrl = "{% url 'celery_progress:task_status' task_id %}";
99  CeleryProgressBar.initProgressBar(progressUrl);
100});
101```
102
103or
104
105```javascript
106// JQuery
107$(function () {
108  var progressUrl = "{% url 'celery_progress:task_status' task_id %}";
109  CeleryProgressBar.initProgressBar(progressUrl)
110});
111```
112
113### Displaying the result of a task
114
115If you'd like you can also display the result of your task on the front end.
116
117To do that follow the steps below. Result handling can also be customized.
118
119#### Initialize the result block:
120
121This is all that's needed to render the result on the page.
122
123**display_progress.html**
124```html
125<div id="celery-result"></div>
126```
127
128But more likely you will want to customize how the result looks, which can be done as below:
129
130```javascript
131// JQuery
132var progressUrl = "{% url 'celery_progress:task_status' task_id %}";
133
134function customResult(resultElement, result) {
135  $( resultElement ).append(
136    $('<p>').text('Sum of all seconds is ' + result)
137  );
138}
139
140$(function () {
141  CeleryProgressBar.initProgressBar(progressUrl, {
142    onResult: customResult,
143  })
144});
145```
146
147## Customization
148
149The `initProgressBar` function takes an optional object of options. The following options are supported:
150
151| Option | What it does | Default Value |
152|--------|--------------|---------------|
153| pollInterval | How frequently to poll for progress (in milliseconds) | 500 |
154| progressBarId | Override the ID used for the progress bar | 'progress-bar' |
155| progressBarMessageId | Override the ID used for the progress bar message | 'progress-bar-message' |
156| progressBarElement | Override the *element* used for the progress bar. If specified, progressBarId will be ignored. | document.getElementById(progressBarId) |
157| progressBarMessageElement | Override the *element* used for the progress bar message. If specified, progressBarMessageId will be ignored. | document.getElementById(progressBarMessageId) |
158| resultElementId | Override the ID used for the result | 'celery-result' |
159| resultElement | Override the *element* used for the result. If specified, resultElementId will be ignored. | document.getElementById(resultElementId) |
160| onProgress | function to call when progress is updated | onProgressDefault |
161| onSuccess | function to call when progress successfully completes | onSuccessDefault |
162| onError | function to call on a known error with no specified handler | onErrorDefault |
163| onRetry | function to call when a task attempts to retry | onRetryDefault |
164| onIgnored | function to call when a task result is ignored | onIgnoredDefault |
165| onTaskError | function to call when progress completes with an error | onError |
166| onNetworkError | function to call on a network error (ignored by WebSocket) | onError |
167| onHttpError | function to call on a non-200 response (ignored by WebSocket) | onError |
168| onDataError | function to call on a response that's not JSON or has invalid schema due to a programming error | onError |
169| onResult | function to call when returned non empty result | CeleryProgressBar.onResultDefault |
170| barColors | dictionary containing color values for various progress bar states. Colors that are not specified will defer to defaults | barColorsDefault |
171
172The `barColors` option allows you to customize the color of each progress bar state by passing a dictionary of key-value pairs of `state: #hexcode`. The defaults are shown below.
173
174| State | Hex Code | Image Color |
175|-------|----------|:-------------:|
176| success | #76ce60 | ![#76ce60](https://placehold.it/15/76ce60/000000?text=+) |
177| error | #dc4f63 | ![#dc4f63](https://placehold.it/15/dc4f63/000000?text=+) |
178| progress | #68a9ef | ![#68a9ef](https://placehold.it/15/68a9ef/000000?text=+) |
179| ignored | #7a7a7a | ![#7a7a7a](https://placehold.it/15/7a7a7a/000000?text=+) |
180
181# WebSocket Support
182
183Additionally, this library offers WebSocket support using [Django Channels](https://channels.readthedocs.io/en/latest/)
184courtesy of [EJH2](https://github.com/EJH2/).
185
186A working example project leveraging WebSockets is [available here](https://github.com/EJH2/cp_ws-example).
187
188To use WebSockets, install with `pip install celery-progress[websockets,redis]` or
189`pip install celery-progress[websockets,rabbitmq]` (depending on broker dependencies).
190
191See `WebSocketProgressRecorder` and `websockets.js` for details.
192