title: How to serve static files in Flask
body: <p>So this is embarrassing. I've got an application that I threw together in <code>Flask</code> and for now it is just serving up a single static HTML page with some links to CSS and JS. And I can't find where in the documentation <code>Flask</code> describes returning static files. Yes, I could use <code>render_template</code> but I know the data is not templatized. I'd have thought <code>send_file</code> or <code>url_for</code> was the right thing, but I could not get those to work. In the meantime, I am opening the files, reading content, and rigging up a <code>Response</code> with appropriate mimetype:</p>
  <pre><code>import os.path
  
  from flask import Flask, Response
  
  
  app = Flask(__name__)
  app.config.from_object(__name__)
  
  
  def root_dir():  # pragma: no cover
      return os.path.abspath(os.path.dirname(__file__))
  
  
  def get_file(filename):  # pragma: no cover
      try:
          src = os.path.join(root_dir(), filename)
          # Figure out how flask returns static files
          # Tried:
          # - render_template
          # - send_file
          # This should not be so non-obvious
          return open(src).read()
      except IOError as exc:
          return str(exc)
  
  
  @app.route('/', methods=['GET'])
  def metrics():  # pragma: no cover
      content = get_file('jenkins_analytics.html')
      return Response(content, mimetype=&quot;text/html&quot;)
  
  
  @app.route('/', defaults={'path': ''})
  @app.route('/&lt;path:path&gt;')
  def get_resource(path):  # pragma: no cover
      mimetypes = {
          &quot;.css&quot;: &quot;text/css&quot;,
          &quot;.html&quot;: &quot;text/html&quot;,
          &quot;.js&quot;: &quot;application/javascript&quot;,
      }
      complete_path = os.path.join(root_dir(), path)
      ext = os.path.splitext(path)[1]
      mimetype = mimetypes.get(ext, &quot;text/html&quot;)
      content = get_file(complete_path)
      return Response(content, mimetype=mimetype)
  
  
  if __name__ == '__main__':  # pragma: no cover
      app.run(port=80)
  </code></pre>
  <p>Someone want to give a code sample or url for this? I know this is going to be dead simple.</p>