Skip to main content
https://travis-ci.org/EeroPaukkonen/django_celery_async_view.svg?branch=master https://codecov.io/gh/EeroPaukkonen/django_celery_async_view/branch/master/graph/badge.svg

Asynchronously load view or download file in django. This is done by rendering view or creating file in celery task and polling it in javascript.

Documentation

The full documentation is at https://django_celery_async_view.readthedocs.io.

Quickstart

Install django_celery_async_view:

pip install django_celery_async_view

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'django_celery_async_view',
    ...
)

Features

This package includes two separate features: AsyncView and AsyncDownload.

AsyncView

example_app.views.py

class ExampleView(AsyncDownloadView):
    task = example_view_task

example_app.tasks.py

@shared_task
def example_view_task(user_id):
    return AsyncViewReturnHTML().run(
        user_id=user_id,
        html_string=create_html_example(...)
    )

example_download_page.html

<!-- jQuery before django_celery_async_view -->
<script type="text/javascript" src="{% static "path/to/jquery.js" %}"></script>
<script type="text/javascript" src="{% static "django_celery_async_view/django_celery_async_view.js" %}"></script>
<script>
    var task_id = '{{task_id}}';
    var poll_interval = 10000;
    AsyncViews.initAsyncView(poll_interval, task_id);
</script>

AsyncDownload

example_app.views.py

class ExampleDownloadView(AsyncDownloadView):
    task = my_download_task

example_async_page.html

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

  <script>
      $(document).ready(function(){
          var poll_interval = 5 * 1000;  // 5s
          var task_id = '{{task_id}}';
          AsyncViews.initAsyncView(poll_interval, task_id);
      });
  </script>

example_app.tasks.py

@shared_task
def example_download_task(*args, **kwargs):
  return MyDownloadCreateFile().run(*args, **kwargs)

class ExampleDownloadCreateFile(AbstractAsyncDownloadCreateFile):
  def create_file(self, some_arg):
    # do stuff to create:
    # file_content, filename, mimetype
    return file_content, filename, mimetype

example_download_page.html

<!-- jQuery before django_celery_async_view -->
<script type="text/javascript" src="{% static "path/to/jquery.js" %}"></script>
<script type="text/javascript" src="{% static "django_celery_async_view/django_celery_async_view.js" %}"></script>
<button>DOWNLOAD</button>
<button class="async-download-button"
        data-href="/example-download/" data-poll-interval="5000">
    Async Download
</button>

Configurations

settings.py

ASYNC_VIEW_TEMP_FILE_DURATION_MS = 10 * 60 * 1000  # 10min

Running Example Project

Requires redis. Does not require postgres (tests require postgres)

# setup
# install redis
cd example
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
python manage.py migrate

# run:
# run redis (if not running)
celery worker -A example
python manage.py runserver

Running Tests

Tests are run against example project? Tests require postgres and

  1. env var POSTGRES_PASSWORD set

  2. or no postgres server authentication

virtualenv venv
source venv/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Development

What needs to be fixed for Python3 support

  1. copy celerytest project and set celerytest.__init__ imports to absolute

celerytest.__init__.py:1: in <module>
from config import CELERY_TEST_CONFIG, CELERY_TEST_CONFIG_MEMORY
ImportError: No module named 'config'
  1. AsyncResult(task_id).wait(timeout=5, interval=0.5) and possibly result.get() will break.

example\example\tests\test_async_views.py:50: in phase3
example_view_task.AsyncResult(task_id).wait(timeout=5, interval=0.5)

    if meta:
            self._maybe_set_cache(meta)
            status = meta['status']
            if status in PROPAGATE_STATES and propagate:
>               raise meta['result']
E               TypeError: exceptions must derive from BaseException

celery\result.py:175: TypeError

Credits

Tools used in rendering this package:

History

0.1.0 (2017-11-14)

  • First release on PyPI.

Metadata

Release files for django-celery-async-view 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-celery-async-view 0.1.0
File Size Uploaded
django_celery_async_view-0.1.0.tar.gz 13.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-celery-async-view 0.1.0
File Interpreter ABI Platform
django_celery_async_view-0.1.0-py2-none-any.whl Python 2 none any Details

Total release size: 28.2 kB

Release files / django_celery_async_view-0.1.0.tar.gz

Download URL django_celery_async_view-0.1.0.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a4b520dc578c97f4a830b5c68daf21192990a7b36c8c0ae2c0a73baa95be08ec
BLAKE2b-256 checksum
How to use checksums
00a19166f7ed680176b7349cad6f7a710ee74bedf81970cc3ea611ab0994680f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_celery_async_view-0.1.0-py2-none-any.whl

Download URL django_celery_async_view-0.1.0-py2-none-any.whl
Size 15.1 kB
Tags Python 2
SHA-256 checksum
How to use checksums
3e6a311409e86a5a42de73aae90c7f40f03005827a6aa1151f81414fdd0f2e1e
BLAKE2b-256 checksum
How to use checksums
4c69df17f06d313265c17e45e30f6a1d8e1e7857846bd71c44ffc6e8da4acec9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page