https://github.com/silentsokolov/flask-thumbnails
A simple extension to create a thumbs for the flask framework
https://github.com/silentsokolov/flask-thumbnails
flask python thumbnails
Last synced: 10 months ago
JSON representation
A simple extension to create a thumbs for the flask framework
- Host: GitHub
- URL: https://github.com/silentsokolov/flask-thumbnails
- Owner: silentsokolov
- License: mit
- Created: 2013-04-23T08:15:35.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T16:21:09.000Z (about 2 years ago)
- Last Synced: 2024-04-23T19:23:56.662Z (about 2 years ago)
- Topics: flask, python, thumbnails
- Language: Python
- Homepage:
- Size: 71.3 KB
- Stars: 61
- Watchers: 6
- Forks: 22
- Open Issues: 4
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
.. image:: https://github.com/silentsokolov/flask-thumbnails/actions/workflows/build.yml/badge.svg
:target: https://github.com/silentsokolov/flask-thumbnails/actions?query=workflow%3Abuild+branch%3Amaster
.. image:: https://codecov.io/gh/silentsokolov/flask-thumbnails/branch/master/graph/badge.svg
:target: https://codecov.io/gh/silentsokolov/flask-thumbnails
flask-thumbnails
================
A simple extension to create a thumbs for the Flask
Installation
------------
Use your favorite Python package manager to install the app from PyPI, e.g.
Example:
``pip install flask-thumbnails``
Add ``Thumbnail`` to your extension file:
.. code:: python
from flask import Flask
from flask_thumbnails import Thumbnail
app = Flask(__name__)
thumb = Thumbnail(app)
Add ``THUMBNAIL_MEDIA_ROOT`` and ``THUMBNAIL_MEDIA_URL`` in your settings:
.. code:: python
app.config['THUMBNAIL_MEDIA_ROOT'] = '/home/www/media'
app.config['THUMBNAIL_MEDIA_URL'] = '/media/'
Example usage
-------------
Use in Jinja2 template:
.. code:: html
%20}})
Options
~~~~~~~
``crop='fit'`` returns a sized and cropped version of the image, cropped to the requested aspect ratio and size, `read more `_.
``quality=XX`` changes the quality of the output JPEG thumbnail, default ``90``.
Develop and Production
----------------------
Production
~~~~~~~~~~
In production, you need to add media directory in you web server.
Develop
~~~~~~~
To service the uploaded files need a helper function, where ``/media/`` your settings ``app.config['THUMBNAIL_MEDIA_URL']``:
.. code:: python
from flask import send_from_directory
@app.route('/media/')
def media_file(filename):
return send_from_directory(app.config['THUMBNAIL_MEDIA_THUMBNAIL_ROOT'], filename)
Option settings
---------------
If you want to store the thumbnail in a folder other than the ``THUMBNAIL_MEDIA_THUMBNAIL_ROOT``, you need to set it manually:
.. code:: python
app.config['THUMBNAIL_MEDIA_THUMBNAIL_ROOT'] = '/home/www/media/cache'
app.config['THUMBNAIL_MEDIA_THUMBNAIL_URL'] = '/media/cache/'
app.config['THUMBNAIL_STORAGE_BACKEND'] = 'flask_thumbnails.storage_backends.FilesystemStorageBackend'
app.config['THUMBNAIL_DEFAULT_FORMAT'] = 'JPEG'
Migrate 0.X to 1.X
------------------
Since version 1.X all settings have a prefix ``THUMBNAIL_``. Example: ``MEDIA_ROOT`` -> ``THUMBNAIL_MEDIA_ROOT``.