I have a html file which references static object like this
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.
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)
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.
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))
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.