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)
Answer from codegeek on Stack Overflowsend_from_directory is ignoring the path value being supplied
Using send_file on an entire directory
Save and display static file with flask : Forums : PythonAnywhere
Problem retreiving uploaded files with send_from_directory
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)
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.
Hello! I'm wondering if it is possible to let the user download a directory/folder using send_file. When I try to pass in a directory instead of a file to send_file, I get a permission error in Visual Studio Code.
Edit: Solved! Here's how I did it in case anyone ever has this issue.
If you make the folder into a zip file (using shutil.make_archive for example) then you can pass in the zip file to flask.send_file() and it'll work.
Thank you u/weebsnore for your help!
Hi everyone,
For a project, I architectured my Flask app as a package. So at the root, I'm having a config.py
file where I declare the uploads folder like this:
UPLOAD_FOLDER = 'myapp/uploads'
Now from myapp/routes.py, I manage to upload files in the right folder when creating a post, but then I try to declare a route to retrieve these files with:
@app.route('/uploads/') def upload(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
But when displaying the html page where I call url_for('upload', filename=post.picture) I get a 404 on the image... like if Flask was trying to access /myapp/uploads from /myapp
Does someone have an idea?
I tried to hardcode "uploads" into the first parameter of send_from_directory with success. like this:
@app.route('/uploads/<filename>')
def upload(filename):
return send_from_directory('uploads', filename)
But it's dirty and will break if I ever change the UPLOAD_FOLDER value in config.py. So I'm looking for a clean solution to this.