Skip to main content

Asynchronously load view or download file in django.

Project description

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.

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

django_celery_async_view-0.1.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

django_celery_async_view-0.1.0-py2-none-any.whl (15.1 kB view details)

Uploaded Python 2

File details

Details for the file django_celery_async_view-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_celery_async_view-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4b520dc578c97f4a830b5c68daf21192990a7b36c8c0ae2c0a73baa95be08ec
MD5 730458af0f2d78e81cbfff897624eb23
BLAKE2b-256 00a19166f7ed680176b7349cad6f7a710ee74bedf81970cc3ea611ab0994680f

See more details on using hashes here.

File details

Details for the file django_celery_async_view-0.1.0-py2-none-any.whl.

File metadata

File hashes

Hashes for django_celery_async_view-0.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 3e6a311409e86a5a42de73aae90c7f40f03005827a6aa1151f81414fdd0f2e1e
MD5 747419e45f30f6839f962bf164f22aad
BLAKE2b-256 4c69df17f06d313265c17e45e30f6a1d8e1e7857846bd71c44ffc6e8da4acec9

See more details on using hashes here.

Supported by

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