Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Flask-Session be installed with optional dependencies. #228

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Navigate to the project directory and run the following commands:
Create and activate a virtual environment

.. code-block:: bash

$ python -m venv .venv
$ source .venv/bin/activate

Install dependencies

.. code-block:: bash

$ pip install -r requirements/dev.in
$ pip install -r requirements/dev.txt
$ pip install -r requirements/docs.in

Install the package in editable mode
Expand Down Expand Up @@ -47,11 +47,11 @@ or
Run the tests together or individually

.. code-block:: bash

$ pytest tests
$ pytest tests/test_basic.py

For easier startup and teardown of storage for testing you may use
For easier startup and teardown of storage for testing you may use

.. code-block:: bash

Expand All @@ -62,18 +62,18 @@ Using rye
~~~~~~~~~~~

.. code-block:: bash

$ rye pin 3.11
$ rye sync


.. code-block:: bash

$ rye run python examples/hello.py


etc.

Pull requests
--------------
Please check previous pull requests before submitting a new one.
Please check previous pull requests before submitting a new one.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Contributors

- [eiriklid](https://github.com/eiriklid)
- [nebolax](https://github.com/nebolax)
- [Taragolis](https://github.com/Taragolis)
- [Lxstr](https://github.com/Lxstr)
Expand Down
61 changes: 44 additions & 17 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ Install from PyPI using an installer such as pip:

$ pip install Flask-Session

Flask-Session's only required dependency is msgspec for serialization, which has no sub-dependencies.
Flask-Session's only required dependency is msgspec for serialization, which has no sub-dependencies.

However, you also need to choose a storage type and install an appropriate client library so the app can communicate with storage. For example, if you want to use Redis as your storage, you will need to install the redis-py client library:
However, you also need to choose a storage type and install an appropriate client library so the app can communicate with storage.
For example, if you want to use Redis as your storage, you will need to install the redis-py_ library either directly or as an optional dependency like below:

.. code-block:: bash

$ pip install redis
$ pip install Flask-Session[redis]

Redis is the recommended storage type for Flask-Session, as it has the most complete support for the features of Flask-Session with minimal configuration.

Expand All @@ -23,32 +24,58 @@ Redis is the recommended storage type for Flask-Session, as it has the most comp
Flask-Session versions below 1.0.0 (not yet released), use pickle_ as the default serializer, which may have security implications in production if your storage is ever compromised.


Direct support
---------------
Available storage options
^^^^^^^^^^^^^^^^^^^^^^^^^

To install Flask-Session with support for a specific storage backend, use the following command, replacing ``<storage-option>`` with your chosen backend from the list below:

.. code-block:: bash

pip install Flask-Session[<storage-option>]

Available storage options and their corresponding ``<storage-option>`` values are:

Flask-Session has an increasing number of directly supported storage and client libraries.

.. list-table::
:header-rows: 1
:align: left

* - Storage
- Client Library
* - Redis
- <storage-option>
- Default client library
- Alternative client libraries
* - **Redis**
- ``redis``
- redis-py_
* - Memcached
- pylibmc_, python-memcached_, libmc_ or pymemcache_
* - MongoDB
-
* - **Memcached**
- ``memcached``
- pymemcache_
- pylibmc_, python-memcached_, libmc_
* - **MongoDB**
- ``mongodb``
- pymongo_
* - SQL Alchemy
-
* - **CacheLib**
- ``cachelib``
- cachelib_
-
* - **SQLAlchemy**
- ``sqlalchemy``
- flask-sqlalchemy_
-
* - **DynamoDB**
- ``dynamodb``
- boto3_
-

Other libraries may work if they use the same commands as the ones listed above.
Other storage backends might be compatible with Flask-Session as long as they adhere to the command interfaces used by the libraries listed above.

Cachelib
--------

Flask-Session also indirectly supports storage and client libraries via cachelib_, which is a wrapper around various cache libraries. You must also install cachelib_ itself to use these.
Flask-Session also indirectly supports storage and client libraries via cachelib_, which is a wrapper around various cache libraries.
You must also install cachelib_ itselfand the relevant client library to use these.

.. list-table::
:header-rows: 1
Expand All @@ -70,7 +97,7 @@ Flask-Session also indirectly supports storage and client libraries via cachelib
- pymongo_
* - DynamoDB
- boto3_


.. warning::

Expand All @@ -82,10 +109,10 @@ Flask-Session also indirectly supports storage and client libraries via cachelib
.. _python-memcached: https://github.com/linsomniac/python-memcached
.. _pymemcache: https://github.com/pinterest/pymemcache
.. _pymongo: https://pymongo.readthedocs.io/en/stable
.. _Flask-SQLAlchemy: https://github.com/pallets-eco/flask-sqlalchemy
.. _flask-sqlalchemy: https://github.com/pallets-eco/flask-sqlalchemy
.. _cachelib: https://cachelib.readthedocs.io/en/stable/
.. _google.appengine.api.memcached: https://cloud.google.com/appengine/docs/legacy/standard/python/memcache
.. _boto3: https://boto3.amazonaws.com/v1/documentation/api/latest/index.html
.. _libmc: https://github.com/douban/libmc
.. _uwsgi: https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
.. _pickle: https://docs.python.org/3/library/pickle.html
.. _pickle: https://docs.python.org/3/library/pickle.html
26 changes: 25 additions & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,28 @@ Or, if you prefer to directly set parameters rather than using the configuration
app = Flask(__name__)

redis = Redis(host='localhost', port=6379)
app.session_interface = RedisSessionInterface(client=redis)
app.session_interface = RedisSessionInterface(client=redis)


Using CacheLib as a session backend
------------------------------------

.. note::

FileSystemSession was recently deprecated in favor of CacheLib, which is what is was using under the hood.

The following example demonstrates how to use CacheLib as a session backend with the file system cache. This might be useful for rapid development or testing.

.. code-block:: python

from flask import Flask, session
from flask_session import Session
from cachelib.file import FileSystemCache

app = Flask(__name__)

SESSION_TYPE = 'cachelib'
SESSION_SERIALIZATION_FORMAT = 'json'
SESSION_CACHELIB = FileSystemCache(threshold=500, cache_dir="/sessions"),
app.config.from_object(__name__)
Session(app)
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ dependencies = [
]
dynamic = ["version"]

[project.optional-dependencies]
cachelib = ["cachelib>=0.10.2"]
memcached = ["pymemcache"]
mongodb = ["pymongo>=4.6.2"]
redis = ["redis>=5.0.3"]
sqlalchemy = ["flask-sqlalchemy>=3.0.5"]
all = ["Flask-Session[cachelib, memcached, mongodb, redis, sqlalchemy]"]

[project.urls]
Documentation = "https://flask-session.readthedocs.io"
Changes = "https://flask-session.readthedocs.io/changes.html"
Expand Down
2 changes: 1 addition & 1 deletion src/flask_session/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .defaults import Defaults

__version__ = "0.7.0"
__version__ = "0.8.0"


class Session:
Expand Down
Loading