Snippet List
This is my POC code for a progress bar backend (for delivering JSON to some AJAX frontend) in Django using Django-Celery (with RabbitMQ). It is quite concise, yet it took me a while to get there because Celery's Documentation is IMHO rather puristic. Web development is meant to be copy-paste! Here you go!
This snippet in the one to improve the method described in [http://djangosnippets.org/snippets/679/](http://djangosnippets.org/snippets/679/). It uses some jquery extensions to make the task more easier: **jquery.timers.js**, **jquery.progressbar.js**, **jquery.form.js**.
- ajax
- javascript
- jquery
- upload
- file
- progress
- jquery extension
Call a function for each element in a queryset (actually, any list).
Features:
* stable memory usage (thanks to Django paginators)
* progress indicators
* wraps batches in transactions
* can take managers or even models (e.g., `Assertion.objects`)
* warns about `DEBUG`.
* handles failures of single items without dying in general.
* stable even if items are added or removed during processing (gets a list of ids at the start)
Returns a `Status` object, with the following interesting attributes
* `total`: number of items in the queryset
* `num_successful`: count of successful items
* `failed_ids`: list of ids of items that failed
- queryset
- progress
- callback
- map
- foreach
- iterator
Another sample of how to integrate Django and jQuery.
===
This starts a function in views.py that takes a long time to finish. It sets a session variable so that another function can report on the situation. We use jquery and ajax to 'pull' that data from Django so as to provide a progress report.
I don't yet know how to background a long-running process, but this is an okay stop-gap method to use. I hope.
\d
- ajax
- json
- jquery
- data
- progress
- pull
This snippet is an example of an ajax progress bar (using jquery) that you might use in conjunction with <http://www.djangosnippets.org/snippets/678/>.
1. Generates a uuid and adds X-Progress-ID to the forms action url.
2. Adds the progress bar to the page. (you'll have to add some css styling for this)
3. Makes period ajax requests to update the progress bar.
- ajax
- javascript
- upload
- file
- progress
Ticket [#2070](http://code.djangoproject.com/ticket/2070) allows you to create your own file upload handlers. Here's an example handler that tracks a file's upload so you might display a progress meter after form submission.
The snippet has two parts, the upload handler which tracks progress, and an upload_progress view used to report back to the browser.
The upload handler uses [django's cache framework](http://www.djangoproject.com/documentation/cache/#the-low-level-cache-api) to store the data, which can then be retrieved by the view to send back to the browser.
Note: Your form's http post request must have a query parameter (X-Progress-ID) sent along with it, which should contain a unique key to identify the upload. That key will also be sent with your ajax requests to the upload_progress view to retrieve the progress data.
Setup: place the UploadProgressCachedHandler anywhere you like on the python path, and add to your settings.py:
from django.conf import global_settings
FILE_UPLOAD_HANDLERS = ('path.to.UploadProgressCachedHandler', ) + \
global_settings.FILE_UPLOAD_HANDLERS
Set up the upload_progress view in any of your apps along with a corresponding entry in your urlconf.
Here's some javascript example code to make the ajax requests and display the progress meter: <http://www.djangosnippets.org/snippets/679/>
.
6 snippets posted so far.