How do I make Flask stream a static file with HTTP 206 Partial Content?
flask, google-chrome, http, python, werkzeug
Solution
I had the same problem when serving my video files and I found the solution by digging into the source code of `Werkzeug`. I solved it by adding the flag `conditional=True` in the `send_from_directory` function as follows:
@app.route('/uploads/<filename>')
def uploaded_file(filename):
"""Endpoint to serve uploaded videos
Use `conditional=True` in order to support range requests necessary for
seeking videos.
"""
return send_from_directory(app.config['UPLOAD_FOLDER'], filename,
conditional=True)
Problem
I want to use a looping video on a site made powered by Flask. Apparently, Chrome will not loop the video, unless it was streamed with an HTTP 206 code being returned. Flask, however, always returns this static file with an HTTP 200. How do I stream static content from my Flask project (hosted on Heroku, for the record) to make the video correctly loop in Chrome?