Skip to content

Commit

Permalink
Allow modification of flask app configs (#1030)
Browse files Browse the repository at this point in the history
* Allow env vars to change flask app behavior

* Run linter

* Convert from string to boolean

* Update documentation
  • Loading branch information
kparaju authored and axsaucedo committed Nov 5, 2019
1 parent eae8d85 commit cd8a2d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doc/source/python/python_wrapping_docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ The service type being created. Available options are:

Set either to 0 or 1. Default is 0. If set to 1 then your model will be saved periodically to redis and loaded from redis (if exists) or created fresh if not.

### FLASK_JSONIFY_PRETTYPRINT_REGULAR

Sets the flask application configuration `JSONIFY_PRETTYPRINT_REGULAR` for the REST API. Available options are `True`
or `False`. If nothing is specified, flask's default value is used.

### FLASK_JSON_SORT_KEYS

Sets the flask application configuration `JSON_SORT_KEYS` for the REST API. Available options are `True` or `False`.
If nothing is specified, flask's default value is used.

## Creating different service types

Expand Down
20 changes: 20 additions & 0 deletions python/seldon_core/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def get_rest_microservice(user_model):
app = Flask(__name__, static_url_path="")
CORS(app)

_set_flask_app_configs(app)

if hasattr(user_model, "model_error_handler"):
logger.info("Registering the custom error handler...")
app.register_blueprint(user_model.model_error_handler)
Expand Down Expand Up @@ -98,6 +100,24 @@ def Aggregate():
return app


def _set_flask_app_configs(app):
"""
Set the configs for the flask app based on environment variables
:param app:
:return:
"""
env_to_config_map = {
"FLASK_JSONIFY_PRETTYPRINT_REGULAR": "JSONIFY_PRETTYPRINT_REGULAR",
"FLASK_JSON_SORT_KEYS": "JSON_SORT_KEYS",
}

for env_var, config_name in env_to_config_map.items():
if os.environ.get(env_var):
# Environment variables come as strings, convert them to boolean
bool_env_value = os.environ.get(env_var).lower() == "true"
app.config[config_name] = bool_env_value


# ----------------------------
# GRPC
# ----------------------------
Expand Down

0 comments on commit cd8a2d0

Please sign in to comment.