Performance: Serving images
This affects page load speed by controlling how quickly images appear and how much data the browser downloads.
from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/image') def image(): response = send_from_directory('static', 'optimized_image.webp') response.cache_control.max_age = 86400 # cache for 1 day return response
from flask import Flask, send_file app = Flask(__name__) @app.route('/image') def image(): return send_file('static/large_image.png')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Serving large uncompressed images | Minimal | 0 | High decode and paint cost | [X] Bad |
| Serving optimized compressed images with caching | Minimal | 0 | Low decode and paint cost | [OK] Good |
send_from_directory in a Flask app when serving images?send_from_directorysend_from_directory in a Flask app?from flask import function_name.send_from_directory, so the import is from flask import send_from_directory./image/cat.png if the file exists in the 'static/images' folder?from flask import Flask, send_from_directory
app = Flask(__name__)
@app.route('/image/<filename>')
def image(filename):
return send_from_directory('static/images', filename)from flask import Flask, send_from_directory
app = Flask(__name__)
@app.route('/img/<filename>')
def img(filename):
return send_from_directory('image', filename)/var/www/uploads. Which is the best way to serve these images securely?Pass the test and this pattern moves unknown