Reputation: 371
I have a html file which references static object like this
<img src="img/snacks.png">
<link href="css/bluestrap.css" rel="stylesheet">
Hence the browser tries to call this via and flask fails to do so
http://127.0.0.1:5000/img/snacks.png
There are lots of such references across multiple files hence changing the references is not possible. How do i serve these static files from FLASK
I have copied all these static files to the 'static' folder and tried this
@app.route('/<path:filename>')
def send_file(filename):
return send_from_directory('/static', filename)
However this does not work, Is there any other way to do this ? or what am i doing wrong ?
Upvotes: 33
Views: 89025
Reputation: 1
This answer is for Flask Version 2.3 this is reference for documentation https://flask.palletsprojects.com/en/latest/api/#flask.send_from_directory this an example
@app.route('/download:')
def download():
return send_from_directory(directory='static/files', path="cheat_sheet.pdf", as_attachment=False)
Upvotes: 0
Reputation: 11
Use:
@app.route('/<path:filename>')
def send_file(filename):
from pathlib import Path
root = Path('.')
folder_path = root / 'static'
return send_from_directory(folder_path, filename, as_attachment=True)
Upvotes: 1
Reputation: 705
You should remove "/" before "/static" in your code:
send_from_directory('/static', filename)
change it to:
send_from_directory('static', filename)
However, in some environments, the server itself does not let running this command on the static folder/directory. Because it is set to be a static file server. For example, with the google cloud, you should build a directory called tmp beside the static folder and then do the send_from_directory command with the tmp folder:
return(send_from_directory('tmp', filename))
Upvotes: 2
Reputation: 33289
In production, you don't want to serve static files using the flask server. I suggest you use a proper web server to do that.
For dev, since you don't want to use url_for
, you can try to initialize your flask app as below. This way, flask knows where your static files are.
app = Flask(__name__, static_folder='static')
@app.route('/<path:filename>')
def send_file(filename):
return send_from_directory(app.static_folder, filename)
See this post with a lot of info Static files in Flask - robot.txt, sitemap.xml (mod_wsgi)
Upvotes: 34
Reputation: 8244
I think a better way to do this would be:
import flask
# ...
@app.route('/img/<fname>')
def legacy_images(fname):
return flask.redirect(flask.url_for('static', filename='img/' + fname), code=301)
Instead of sending the files on two different locations, this would do a permanent redirect to the proper URL. As others have said, it's also a good idea to serve static files directly with nginx or Apache.
Upvotes: 3
Reputation: 3923
Don't use Flask's built-in server in production. It is for development only! And don't use Flask to serve static assets. It's slow! In production use a webserver in front of Flask like apache2, nginx or lighttpd. These servers are able to rewrite a URL and serve static assets.
How to deploy Flask: http://flask.pocoo.org/docs/deploying/
How to rewrite a URL: apache2, nginx, lighttpd.
Upvotes: 7
Reputation: 159865
If you look at the docs for send_from_directory
you'll see that it takes the path to the directory in which the files are held on disk. Unless you have your image files saved in a root-level directory named static
, you'll want to update your file path:
send_from_directory("/some/path/to/static", "my_image_file.jpg")
That being said, if you are using this for anything that will be under any load, it is better to ensure that your web server serves the files, rather than serving static files from your application.
Upvotes: 14