Asynchronously load view or download file in django.
Project description
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
env var POSTGRES_PASSWORD set
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
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'
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_celery_async_view-0.1.0.tar.gz
.
File metadata
- Download URL: django_celery_async_view-0.1.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4b520dc578c97f4a830b5c68daf21192990a7b36c8c0ae2c0a73baa95be08ec |
|
MD5 | 730458af0f2d78e81cbfff897624eb23 |
|
BLAKE2b-256 | 00a19166f7ed680176b7349cad6f7a710ee74bedf81970cc3ea611ab0994680f |
File details
Details for the file django_celery_async_view-0.1.0-py2-none-any.whl
.
File metadata
- Download URL: django_celery_async_view-0.1.0-py2-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e6a311409e86a5a42de73aae90c7f40f03005827a6aa1151f81414fdd0f2e1e |
|
MD5 | 747419e45f30f6839f962bf164f22aad |
|
BLAKE2b-256 | 4c69df17f06d313265c17e45e30f6a1d8e1e7857846bd71c44ffc6e8da4acec9 |