Skip to content

Instantly share code, notes, and snippets.

@omry
Created June 11, 2020 09:34
Show Gist options
  • Save omry/7fab52addf24fe42c2fefd348387e1d0 to your computer and use it in GitHub Desktop.
Save omry/7fab52addf24fe42c2fefd348387e1d0 to your computer and use it in GitHub Desktop.
Hydra flask example
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.8 (test)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="49" name="Python" />
</Languages>
</inspection_tool>
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="11">
<item index="0" class="java.lang.String" itemvalue="omegaconf" />
<item index="1" class="java.lang.String" itemvalue="protobuf" />
<item index="2" class="java.lang.String" itemvalue="gorilla" />
<item index="3" class="java.lang.String" itemvalue="alembic" />
<item index="4" class="java.lang.String" itemvalue="databricks-cli" />
<item index="5" class="java.lang.String" itemvalue="querystring_parser" />
<item index="6" class="java.lang.String" itemvalue="sqlalchemy" />
<item index="7" class="java.lang.String" itemvalue="Flask" />
<item index="8" class="java.lang.String" itemvalue="docker" />
<item index="9" class="java.lang.String" itemvalue="sqlparse" />
<item index="10" class="java.lang.String" itemvalue="prometheus-flask-exporter" />
</list>
</value>
</option>
</inspection_tool>
</profile>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/flask-test.iml" filepath="$PROJECT_DIR$/.idea/flask-test.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectViewState">
<option name="autoscrollFromSource" value="true" />
<option name="autoscrollToSource" value="true" />
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showExcludedFiles" value="false" />
<option name="showLibraryContents" value="true" />
</component>
</project>
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment