Skip to main content

Asynchronous thumbnail generation for sorl-thumbnail using Celery

Project description

Django Sorl Async Thumbnails

Asynchronous thumbnail generation for sorl-thumbnail using Celery.

Installation

pip install django-sorl-async-thumbnails

Configuration

  1. Add sorl_async_thumbnails to INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'sorl.thumbnail',
    'sorl_async_thumbnails',
    ...
]
  1. Configure Celery in your project.

  2. Add settings to settings.py:

# Default settings
THUMBNAIL_BACKEND = 'sorl_async_thumbnails.backend.AsyncThumbnailBackend' #Set AsyncThumbnailBackend as backend for sorl.thumbnails
ASYNC_THUMBNAILS_BY_DEFAULT = False  # Generate thumbnails asynchronously by default
ASYNC_THUMBNAILS_PLACEHOLDER = True  # Use placeholder during generation
ASYNC_THUMBNAILS_PLACEHOLDER_STATICFILE = 'placeholder.jpg'  # Path to placeholder image
ASYNC_THUMBNAILS_GENERATE_PLACEHOLDER_THUMB = True  # Generate thumbnail for placeholder
ASYNC_THUMBNAILS_STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'  # Storage for placeholder

Usage

To generate thumbnails in synchronous mode regardless of settings, use the create_async=False keyword

Example

# In your model
from sorl.thumbnail import get_thumbnail 

class MyModel(models.Model):
    image = models.ImageField(upload_to='images/')
    
    def get_thumbnail(self):
        backend = AsyncThumbnailBackend()
        # If ASYNC_THUMBNAILS_BY_DEFAULT is True, this will generate thumbnail asynchronously
        return get_thumbnail(self.image, '100x100')
    
    def get_thumbnail_sync(self):
        backend = AsyncThumbnailBackend()
        # Force synchronous generation
        return get_thumbnail(self.image, '100x100', create_async=False)
    
    def get_thumbnail_async(self):
        backend = AsyncThumbnailBackend()
        # Force asynchronous generation
        return get_thumbnail(self.image, '100x100', create_async=True)

License

MIT

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_sorl_async_thumbnails-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

django_sorl_async_thumbnails-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_sorl_async_thumbnails-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9984a5e09e8d8261646712fe81b0ea8b43baea66642c5e087dd581e0005a6d5d
MD5 9a85f5c748054a480825882c895954b3
BLAKE2b-256 3c31677c0c6cf40f1751386d194f8aa4e936756640f81042ffede5ebf493eeaf

See more details on using hashes here.

File details

Details for the file django_sorl_async_thumbnails-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sorl_async_thumbnails-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 afc1b2508540669778ec56c18cfdccf4f872080cb3367d35eeae8da6384ba23b
MD5 45ec94e0fdfed741a4879c4c444122b6
BLAKE2b-256 7bc0ce99614fe9baf86672ee8ce989853b874b325b7a80c61f7e65007d4d7c28

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