Skip to content

Instantly share code, notes, and snippets.

@omry
Created June 11, 2020 09:34
Show Gist options
  • Save omry/24c6065a8470e6ee67c48d02e235eec1 to your computer and use it in GitHub Desktop.
Save omry/24c6065a8470e6ee67c48d02e235eec1 to your computer and use it in GitHub Desktop.
Hydra flask example
server:
host: 127.0.0.1
port: 8081
app:
username: ${env:USER}
flask:
ENV: production
DEBUG: false
TESTING: false
PROPAGATE_EXCEPTIONS: null
PRESERVE_CONTEXT_ON_EXCEPTION: null
SECRET_KEY: null
PERMANENT_SESSION_LIFETIME: datetime.timedelta(days=31)
USE_X_SENDFILE: false
SERVER_NAME: null
APPLICATION_ROOT: /
SESSION_COOKIE_NAME: session
SESSION_COOKIE_DOMAIN: null
SESSION_COOKIE_PATH: null
SESSION_COOKIE_HTTPONLY: True
SESSION_COOKIE_SECURE: false
SESSION_COOKIE_SAMESITE: null
SESSION_REFRESH_EACH_REQUEST: True
MAX_CONTENT_LENGTH: null
SEND_FILE_MAX_AGE_DEFAULT: datetime.timedelta(seconds=43200)
TRAP_BAD_REQUEST_ERRORS: null
TRAP_HTTP_EXCEPTIONS: false
EXPLAIN_TEMPLATE_LOADING: false
PREFERRED_URL_SCHEME: http
JSON_AS_ASCII: True
JSON_SORT_KEYS: True
JSONIFY_PRETTYPRINT_REGULAR: false
JSONIFY_MIMETYPE: application/json
TEMPLATES_AUTO_RELOAD: null
MAX_COOKIE_SIZE: 4093
hydra:
# disable changing of cwd as it interferes with flask auto-restart
run:
dir: .
# output files creation
output_subdir: null
import hydra
from omegaconf import DictConfig, OmegaConf, open_dict
from flask import Flask
from flask import current_app
app = Flask(__name__)
@app.route("/")
def hello_world():
cfg = current_app.config["config"]
return f"Hello {cfg.app.username}"
@hydra.main(config_name="config")
def main(cfg: DictConfig):
app.config.from_mapping(cfg.flask)
with open_dict(cfg):
del cfg["flask"]
app.config["config"] = cfg
app.run(host=cfg.server.host, port=cfg.server.port, debug=True)
if __name__ == "__main__":
main()
@jonfreedman
Copy link

Could you share any pointers on how to extend this to be launched via a wsgi.py file?

@omry
Copy link
Author

omry commented Sep 25, 2020

I am not really working on web apps and just was just a quick hack.

If you can't make @hydra.main() work for your use case take a look at the Compose API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment