Skip to content

Instantly share code, notes, and snippets.

@debuggerboy
Created September 6, 2015 09:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debuggerboy/d815cf8c45814d78f376 to your computer and use it in GitHub Desktop.
Save debuggerboy/d815cf8c45814d78f376 to your computer and use it in GitHub Desktop.
Serving Static Files From Root “/” and not “/static” Using Flask
There’s some discussion about how to do this here, involving Werkzeug’s middleware.
http://stackoverflow.com/questions/4239825/static-files-in-flask-robot-txt-sitemap-xml-mod-wsgi
This is another way to do it, assuming you have a folder called “static” under where the main application .py file is located:
from flask import Flask, request
app = Flask(__name__, static_url_path='')
@app.route('/')
def root():
return app.send_static_file('index.html')
By setting static_url_path to a blank string, it means any accesses to unrouted root URLs, will attempt to grab the associated file out of the static folder. What it also means is that if you have HTML in that folder (as would be the case if you’re using Node or some other server), the hrefs do not look like “/static/js/something.js” but just “/js/something.js”, which may be useful.
@kind03
Copy link

kind03 commented Feb 7, 2023

Thanks a lot for pointing that out!

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