👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
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.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How to serve static files in Flask, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to serve static files in Flask
- Deutsch How to serve static files in Flask
- Français How to serve static files in Flask
- Español How to serve static files in Flask
- Türk How to serve static files in Flask
- Русский How to serve static files in Flask
- Português How to serve static files in Flask
- Polski How to serve static files in Flask
- Nederlandse How to serve static files in Flask
- 中文 How to serve static files in Flask
- 한국어 How to serve static files in Flask
- 日本語 How to serve static files in Flask
- हिन्दी How to serve static files in Flask
Shanghai | 2023-03-29
Thanks for explaining! I was stuck with How to serve static files in Flask for some hours, finally got it done 🤗. I am just not quite sure it is the best method
Vigrinia | 2023-03-29
iat is always a bit confusing 😭 How to serve static files in Flask is not the only problem I encountered. Will get back tomorrow with feedback
Berlin | 2023-03-29
Thanks for explaining! I was stuck with How to serve static files in Flask for some hours, finally got it done 🤗. Will get back tomorrow with feedback