So this is embarrassing. I've got an application that I threw together in Flask
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 Flask
describes returning static files. Yes, I could use render_template
but I know the data is not templatized. I'd have thought send_file
or url_for
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 Response
with appropriate mimetype:
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="text/html") @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def get_resource(path): # pragma: no cover mimetypes = { ".css": "text/css", ".html": "text/html", ".js": "application/javascript", } complete_path = os.path.join(root_dir(), path) ext = os.path.splitext(path)[1] mimetype = mimetypes.get(ext, "text/html") content = get_file(complete_path) return Response(content, mimetype=mimetype) if __name__ == '__main__': # pragma: no cover app.run(port=80)
Someone want to give a code sample or url for this? I know this is going to be dead simple.
Usually, the web server is configured to serve them for you, but during the development, these files are served from static folder in your package or next to your module and it will be available at /static on the application. A special endpoint 'static' is used to generate URL for static files.
In this article we saw how you can easily serve static assets using Flask. You can serve JavaScript, CSS, images as well as other static files. This can be done in a variety of ways, with the simplest being to use the "/static" directory, which Flask will use to serve files to the client.
The preferred method is to use NGINX or another web server to serve static files; they'll be able to do it more efficiently than Flask.
However, you can use send_from_directory
to send files from a directory, which can be pretty convenient in some situations:
from flask import Flask, request, send_from_directory # set the project root directory as the static folder, you can set others. app = Flask(__name__, static_url_path='') @app.route('/js/<path:path>') def send_js(path): return send_from_directory('js', path) if __name__ == "__main__": app.run()
Alternatively, you could use app.send_file
or app.send_static_file
, but this is highly discouraged as it can lead to security risks with user-supplied paths; send_from_directory
was designed to control those risks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With