Skip to main content

An extension which allows you to easily gzip your Sanic responses.

Project description

sanic\_compress
~~~~~~~~~~~~~~~

sanic\_compress is an extension which allows you to easily gzip your
Sanic responses. It is a port of the
`Flask-Compress <https://github.com/libwilliam/flask-compress>`__
extension.

Installation
------------

Install with ``pip``:

``pip install sanic_compress``

Usage
-----

Usage is simple. Simply pass in the Sanic app object to the ``Compress``
class, and responses will be gzipped.

.. code:: python

from sanic import Sanic
from sanic_compress import Compress

app = Sanic(__name__)
Compress(app)

Alternatively, if you want to initialize the ``Compress`` class later,
you can do so with the ``init_app`` method;

.. code:: python

compress = Compress()
app = Flask(__name__)
compress.init_app(app)

Options
-------

Within the Sanic application config you can provide the following
settings to control the behavior of sanic\_compress. None of the
settings are required.

+---------+--------------+----------+
| Option | Description | Default |
+=========+==============+==========+
| ``COMPR | Set the list | ``[``\ \ |
| ESS_MIM | of mimetypes | ``'text |
| ETYPES` | to compress | /html',` |
| ` | here. | `\ \ ``' |
| | | text/css |
| | | ',``\ \ |
| | | ``'text/ |
| | | xml',``\ |
| | | \ ``'ap |
| | | plicatio |
| | | n/json', |
| | | ``\ \ `` |
| | | 'applica |
| | | tion/jav |
| | | ascript' |
| | | ``\ \ `` |
| | | ]`` |
+---------+--------------+----------+
| ``COMPR | Specifies | ``6`` |
| ESS_LEV | the gzip | |
| EL`` | compression | |
| | level (1-9). | |
+---------+--------------+----------+
| ``COMPR | Specifies | ``500`` |
| ESS_MIN | the minimum | |
| _SIZE`` | file size | |
| | threshold | |
| | for | |
| | compressing | |
| | files. | |
+---------+--------------+----------+

A higher ``COMPRESS_LEVEL`` will result in a gzipped response that is
smaller, but the compression will take longer.

Example of using custom configuration:

::

from sanic import Sanic
from sanic_compress import Compress

app = Sanic(__name__)
app.config['COMPRESS_MIMETYPES'] = set(['text/html', 'application/json'])
app.config['COMPRESS_LEVEL'] = 4
app.config['COMPRESS_MIN_SIZE'] = 300
Compress(app)

Note about gzipping static files:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sanic is not at heart a file server. You should consider serving static
files with nginx or on a separate file server.

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

sanic_compress-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for sanic_compress-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a3e8e0bf121b29d43763e8d49117fe892d1de6f3c4a187642f8ae572706e9457
MD5 f4bf96e4f080830e6c891887c7bff99f
BLAKE2b-256 2458c5604e3f4664483bcfbfb6b691df7746ec3abe60f93c06e1dbe9464d7637

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