Skip to content

Instantly share code, notes, and snippets.

@brycepg
Created July 15, 2020 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycepg/593ffb5ce9316d2871c7f24f9de34c24 to your computer and use it in GitHub Desktop.
Save brycepg/593ffb5ce9316d2871c7f24f9de34c24 to your computer and use it in GitHub Desktop.
Dynamically change flask static endpoint for url_for
# 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