Flask settings extension is similar to Django settings.
Project description
# flask-settings
Flask settings extension is similar to Django settings.
# Installation
```bash
pip install flask-settings
```
# How to use
File app/settings/default.py
```python
from flask_settings import BasicConfig
class DefaultConfig(BasicConfig):
SQLALCHEMY_DATABASE_PROTOCOL = 'postgresql'
SQLALCHEMY_DATABASE_HOST = 'localhost'
SQLALCHEMY_DATABASE_PORT = 5432
SQLALCHEMY_DATABASE_USERNAME = 'postgres'
SQLALCHEMY_DATABASE_PASSWORD = 'postgres'
SQLALCHEMY_DATABASE_NAME = 'default'
@property
def SQLALCHEMY_DATABASE_URI(self):
# calculated constant
return '{protocol}://{username}:{password}@{host}:{port}/{db}'.format(
protocol=self.SQLALCHEMY_DATABASE_PROTOCOL,
username=self.SQLALCHEMY_DATABASE_USERNAME,
password=self.SQLALCHEMY_DATABASE_PASSWORD,
host=self.SQLALCHEMY_DATABASE_HOST,
port=self.SQLALCHEMY_DATABASE_PORT,
db=self.SQLALCHEMY_DATABASE_NAME)
```
File app/settings/development.py
```python
from app.settings.default import DefaultConfig
class DevelopmentConfig(DefaultConfig):
DEBUG = True
SQLALCHEMY_DATABASE_NAME = 'development'
```
File app/settings/testing.py
```python
from app.settings.default import DefaultConfig
class TestingConfig(DefaultConfig):
TESTING = True
SQLALCHEMY_DATABASE_NAME = 'testing'
```
File app/settings/production.py
```python
from app.settings.default import DefaultConfig
class ProductionConfig(DefaultConfig):
SQLALCHEMY_DATABASE_HOST = '10.0.0.1'
SQLALCHEMY_DATABASE_USERNAME = 'user'
SQLALCHEMY_DATABASE_PASSWORD = 'password'
SQLALCHEMY_DATABASE_NAME = 'production'
```
File app/application.py
```python
import os
from flask import Flask
from flask_settings import Settings
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
settings = Settings(app)
db = SQLAlchemy(app)
db.create_all(app=app)
@app.route('/')
def index_page():
return 'database name: ' + settings.SQLALCHEMY_DATABASE_NAME
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
Run application in development mode:
```bash
FLASK_SETTINGS="development" python app/application.py
```
Run application tests in testing mode:
```bash
FLASK_SETTINGS="testing" nosetests
```
Run application in production mode:
```bash
FLASK_SETTINGS="production" uwsgi --wsgi-file=app/application.py --callable=app --http=0.0.0.0:5000
```
Flask settings extension is similar to Django settings.
# Installation
```bash
pip install flask-settings
```
# How to use
File app/settings/default.py
```python
from flask_settings import BasicConfig
class DefaultConfig(BasicConfig):
SQLALCHEMY_DATABASE_PROTOCOL = 'postgresql'
SQLALCHEMY_DATABASE_HOST = 'localhost'
SQLALCHEMY_DATABASE_PORT = 5432
SQLALCHEMY_DATABASE_USERNAME = 'postgres'
SQLALCHEMY_DATABASE_PASSWORD = 'postgres'
SQLALCHEMY_DATABASE_NAME = 'default'
@property
def SQLALCHEMY_DATABASE_URI(self):
# calculated constant
return '{protocol}://{username}:{password}@{host}:{port}/{db}'.format(
protocol=self.SQLALCHEMY_DATABASE_PROTOCOL,
username=self.SQLALCHEMY_DATABASE_USERNAME,
password=self.SQLALCHEMY_DATABASE_PASSWORD,
host=self.SQLALCHEMY_DATABASE_HOST,
port=self.SQLALCHEMY_DATABASE_PORT,
db=self.SQLALCHEMY_DATABASE_NAME)
```
File app/settings/development.py
```python
from app.settings.default import DefaultConfig
class DevelopmentConfig(DefaultConfig):
DEBUG = True
SQLALCHEMY_DATABASE_NAME = 'development'
```
File app/settings/testing.py
```python
from app.settings.default import DefaultConfig
class TestingConfig(DefaultConfig):
TESTING = True
SQLALCHEMY_DATABASE_NAME = 'testing'
```
File app/settings/production.py
```python
from app.settings.default import DefaultConfig
class ProductionConfig(DefaultConfig):
SQLALCHEMY_DATABASE_HOST = '10.0.0.1'
SQLALCHEMY_DATABASE_USERNAME = 'user'
SQLALCHEMY_DATABASE_PASSWORD = 'password'
SQLALCHEMY_DATABASE_NAME = 'production'
```
File app/application.py
```python
import os
from flask import Flask
from flask_settings import Settings
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
settings = Settings(app)
db = SQLAlchemy(app)
db.create_all(app=app)
@app.route('/')
def index_page():
return 'database name: ' + settings.SQLALCHEMY_DATABASE_NAME
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
Run application in development mode:
```bash
FLASK_SETTINGS="development" python app/application.py
```
Run application tests in testing mode:
```bash
FLASK_SETTINGS="testing" nosetests
```
Run application in production mode:
```bash
FLASK_SETTINGS="production" uwsgi --wsgi-file=app/application.py --callable=app --http=0.0.0.0:5000
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Flask-Settings-0.0.2.tar.gz
(3.0 kB
view hashes)