Created
July 15, 2020 20:32
-
-
Save brycepg/593ffb5ce9316d2871c7f24f9de34c24 to your computer and use it in GitHub Desktop.
Dynamically change flask static endpoint for url_for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Flask creates a rule for the static endpoint a initialization | |
# this means that setting static_url_path and static_folder | |
# after initilzation do not change the result of url_for('static', filename=...) | |
# app is in instance of the Flask class | |
# Your new static path, update the instance just to keep everything consistent | |
app.static_url_path = "/foo" | |
# Delete the old rule and substitute your own | |
# Note this looks like it could change from implementation | |
# to implementation but I see no other way | |
del app.url_map._rules_by_endpoint['static'] | |
app.add_url_rule( | |
f"{app.static_url_path}/<path:filename>", | |
endpoint="static", | |
host=None, | |
view_func=app.send_static_file, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment