Skip to content

Commit

Permalink
Option to create_app to force loading .env and .flaskenv files with
Browse files Browse the repository at this point in the history
python-dotenv.
  • Loading branch information
makmanalp committed Oct 1, 2018
1 parent 3e4b63c commit 12f5692
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 18 additions & 5 deletions atlas_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from flask import Flask
from flask import cli

from werkzeug.contrib.profiler import ProfilerMiddleware

from .core import db
from .helpers.flask import APIError, handle_api_error, ForgivingJSONEncoder


def load_config(app, additional_config={}):
def load_config(app, overrides={}):
"""Load configuration from environment variable plus from additional
dictionary for test cases."""
dictionary for test cases etc."""
app.config.from_envvar("FLASK_CONFIG")
app.config.update(additional_config)
app.config.update(overrides)
return app


Expand All @@ -31,12 +32,24 @@ def create_db(app, db):
db.create_all()


def create_app(additional_config={}, name="atlas_core", standalone=False, custom_json_encoder=True):
def create_app(additional_config={}, name="atlas_core", standalone=False,
custom_json_encoder=True, load_dotenv=False):
"""App factory. Creates a Flask `app` object and imports extensions, sets
config variables etc."""

app = Flask(name)
app = load_config(app, additional_config)

# Load environment variables from .env and .flaskenv including FLASK_APP
# and FLASK_CONFIG etc etc. The flask 1.0+ CLI (`flask commandname`) does
# this automatically if you have python-dotenv installed, but if you call
# create_app() manually from your own code you need this. This needs to
# happen before pretty much anything, so that we can customize even the
# flask config location with this.
if load_dotenv:
cli.load_dotenv()

# Load config from FLASK_CONFIG env variable.
app = load_config(app, overrides=additional_config)

# Load extensions
db.init_app(app)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ def read(fname, lines=False):
else:
return f.read()


setup(
name="atlas_core",
version="v0.2.7",
version="v1.3",
author="Mali Akmanalp <Harvard CID>",
description=("Core building blocks for atlas projects at CID."),
url="http://github.com/cid-harvard/atlas_core",
Expand Down

0 comments on commit 12f5692

Please sign in to comment.