Skip to main content

Drop in, configurable, dependency-free progress bars for your Django/Celery applications.

Project description

Celery Progress Bars for Django

Drop in, dependency-free progress bars for your Django/Celery applications.

Super simple setup. Lots of customization available.

Demo

Celery Progress Bar demo on Build With Django

Installation

pip install celery-progress

Usage

Prerequisites

First add celery_progress to your INSTALLED_APPS in settings.py.

Then add the following url config to your main urls.py:

from django.urls import re_path
re_path(r'^celery-progress/', include('celery_progress.urls')),  # the endpoint is configurable

Recording Progress

In your task you should add something like this:

from celery import shared_task
from celery_progress.backend import ProgressRecorder
import time

@shared_task(bind=True)
def my_task(self, seconds):
    progress_recorder = ProgressRecorder(self)
    result = 0
    for i in range(seconds):
        time.sleep(1)
        result += i
        progress_recorder.set_progress(i + 1, seconds)
    return result

You can add an optional progress description like this:

  progress_recorder.set_progress(i + 1, seconds, description='my progress description')

You can stop your task with an exception message like this:

  progress_recorder.stop_task(i + 1, seconds, 'my exception message')

Displaying progress

In the view where you call the task you need to get the task ID like so:

views.py

def progress_view(request):
    result = my_task.delay(10)
    return render(request, 'display_progress.html', context={'task_id': result.task_id})

Then in the page you want to show the progress bar you just do the following.

Add the following HTML wherever you want your progress bar to appear:

display_progress.html

<div class='progress-wrapper'>
  <div id='progress-bar' class='progress-bar' style="background-color: #68a9ef; width: 0%;">&nbsp;</div>
</div>
<div id="progress-bar-message">Waiting for progress to start...</div>

Import the javascript file.

display_progress.html

<script src="{% static 'celery_progress/celery_progress.js' %}"></script>

Initialize the progress bar:

// vanilla JS version
document.addEventListener("DOMContentLoaded", function () {
  var progressUrl = "{% url 'celery_progress:task_status' task_id %}";
  CeleryProgressBar.initProgressBar(progressUrl);
});

or

// JQuery
$(function () {
  var progressUrl = "{% url 'celery_progress:task_status' task_id %}";
  CeleryProgressBar.initProgressBar(progressUrl)
});

Displaying the result of a task

If you'd like you can also display the result of your task on the front end.

To do that follow the steps below. Result handling can also be customized.

Initialize the result block:

This is all that's needed to render the result on the page.

display_progress.html

<div id="celery-result"></div>

But more likely you will want to customize how the result looks, which can be done as below:

// JQuery
var progressUrl = "{% url 'celery_progress:task_status' task_id %}";

function customResult(resultElement, result) {
  $( resultElement ).append(
    $('<p>').text('Sum of all seconds is ' + result)
  );
}

$(function () {
  CeleryProgressBar.initProgressBar(progressUrl, {
    onResult: customResult,
  })
});

Customization

The initProgressBar function takes an optional object of options. The following options are supported:

Option What it does Default Value
pollInterval How frequently to poll for progress (in milliseconds) 500
progressBarId Override the ID used for the progress bar 'progress-bar'
progressBarMessageId Override the ID used for the progress bar message 'progress-bar-message'
progressBarElement Override the element used for the progress bar. If specified, progressBarId will be ignored. document.getElementById(progressBarId)
progressBarMessageElement Override the element used for the progress bar message. If specified, progressBarMessageId will be ignored. document.getElementById(progressBarMessageId)
resultElementId Override the ID used for the result 'celery-result'
resultElement Override the element used for the result. If specified, resultElementId will be ignored. document.getElementById(resultElementId)
onProgress function to call when progress is updated CeleryProgressBar.onProgressDefault
onSuccess function to call when progress successfully completes CeleryProgressBar.onSuccessDefault
onError function to call when progress completes with an error CeleryProgressBar.onErrorDefault
onResult function to call when returned non empty result CeleryProgressBar.onResultDefault

WebSocket Support

This library has experimental WebSocket support using Django Channels courtesy of @EJH2.

A working example project leveraging WebSockets is available here.

To use WebSockets, install with pip install celery-progress[websockets,redis] or pip install celery-progress[websockets,rabbitmq] (depending on broker dependencies).

See WebSocketProgressRecorder and websockets.js for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

celery-progress-0.0.10.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

celery_progress-0.0.10-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file celery-progress-0.0.10.tar.gz.

File metadata

  • Download URL: celery-progress-0.0.10.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.5.2

File hashes

Hashes for celery-progress-0.0.10.tar.gz
Algorithm Hash digest
SHA256 3f7b35e1e6c79eec38f5647b024aa74193d0a41d5b47ecbb85b66f9ca68d5261
MD5 ecf6527957551822fdc58ebe2b6c6d69
BLAKE2b-256 703e13d99df34f04cb5329026880ace15b52d45998a4000d9b29b785abd740e4

See more details on using hashes here.

File details

Details for the file celery_progress-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: celery_progress-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.5.2

File hashes

Hashes for celery_progress-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 90941bf3aaeac9333d554a2191fa6cd81ef323472329ace0dd77344ac6aab092
MD5 86f9b840eae1c485a3e3d385278b6681
BLAKE2b-256 7c6494bfb8fdc2c93d4e47548c911ea3fc2c01933c7872e22c208e49c9a91e67

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page