https://github.com/brettlangdon/flask-env
Easily set Flask settings from environment variables
https://github.com/brettlangdon/flask-env
Last synced: 30 days ago
JSON representation
Easily set Flask settings from environment variables
- Host: GitHub
- URL: https://github.com/brettlangdon/flask-env
- Owner: brettlangdon
- License: mit
- Created: 2016-08-07T21:52:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-19T01:59:10.000Z (over 5 years ago)
- Last Synced: 2025-01-24T09:17:36.592Z (3 months ago)
- Language: Python
- Homepage: https://pypi.python.org/pypi/Flask-Env
- Size: 20.5 KB
- Stars: 35
- Watchers: 3
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - brettlangdon/flask-env - Easily set Flask settings from environment variables (Python)
README
Flask-Env
=========.. image:: https://badge.fury.io/py/Flask-Env.svg
:target: https://badge.fury.io/py/Flask-Env
.. image:: https://travis-ci.org/brettlangdon/flask-env.svg?branch=master
:target: https://travis-ci.org/brettlangdon/flask-envEasily set `Flask `_ settings from environment variables.
The reason for using :code:`flask-env` is to be able to follow the `12-factor app `_ suggestions for configuring your application.
With :code:`flask-env` you can define your default configuration options in code and very easily override via environment variables.
Installation
~~~~~~~~~~~~.. code:: bash
pip install Flask-Env
Usage
~~~~~With :code:`flask-env` you will define your configuration as an object and load it into your Flask application via `app.config.from_object `_ method.
Python 2
--------.. code:: python
from flask import Flask
from flask_env import MetaFlaskEnvclass Configuration(object):
__metaclass__ = MetaFlaskEnvDEBUG = False
PORT = 5000app = Flask(__name__)
app.config.from_object(Configuration)Python 3
--------.. code:: python
from flask import Flask
from flask_env import MetaFlaskEnvclass Configuration(metaclass=MetaFlaskEnv):
DEBUG = False
PORT = 5000app = Flask(__name__)
app.config.from_object(Configuration)Overriding environment variables
--------------------------------.. code:: bash
# Export environment variable for shell session
export DEBUG=true# Set explicitly for a specific command execution
PORT=8000 python app.pyConfiguring flask-env
~~~~~~~~~~~~~~~~~~~~~:code:`flask-env` offers two configuration options to determine how/which environment variables are loaded.
ENV_PREFIX
Only consider environment variables that start with this prefix.
The prefix will be removed from the environment variable name when setting in the configuration.
(default: :code:`''`, example: :code:`ENV_PREFIX = 'MYAPP_'`)ENV_LOAD_ALL
Whether or not to load all environment variables for the configuration object.
When :code:`False` only settings predefined on the configuration object are loaded, all others are ignored.
When :code:`True` all environment variables defined in :code:`os.environ` will get loaded into your configuration object.
(default :code:`False`)Setting configuration values
----------------------------You can set the :code:`flask-env` configuration settings directly on your Flask configuration object.
.. code:: python
from flask_env import MetaFlaskEnv
class Configuration(metaclass=MetaFlaskEnv):
ENV_PREFIX = 'MYAPP_'
ENV_LOAD_ALL = False