Daming Lu
Daming Lu

Reputation: 376

Use Flask to serve both static CSS and static video files

I set up a Flask server to render raw MP4 videos. I can play the videos stored outside my Flask project folder by setting app = Flask(__name__, static_folder=<video_folder>). But this will cause Flask to fail to find the CSS files stored inside the project folder for the website. Any solution to solve both? 'static_folder' cannot be a list.

Upvotes: 1

Views: 1580

Answers (1)

Matt L.
Matt L.

Reputation: 3621

Your video folder should be inside the 'static' folder, and you should have your URLs to videos point to 'static/video/file.mov' etc. You can use url_for to make sure the hrefs are constructed properly. See https://flask.palletsprojects.com/en/1.1.x/tutorial/static/ and https://flask.palletsprojects.com/en/1.1.x/quickstart/#url-building

If you cannot move the files for some reason, you can use send_from_directory in your route to do what you're describing:

app.route('/uploads/<path:filename>')
def download_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename, as_attachment=True)

See https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_from_directory

Upvotes: 2

Related Questions