Skip to content

Instantly share code, notes, and snippets.

@mseeks
Created February 13, 2024 02:11
Show Gist options
  • Save mseeks/8746fd697ce518823c17b6da83e4de35 to your computer and use it in GitHub Desktop.
Save mseeks/8746fd697ce518823c17b6da83e4de35 to your computer and use it in GitHub Desktop.
Python Flask snippet for serving a file at an endpoint. This snippet demonstrates how to use the `send_file` method to allow users to download a file when they access a specific route.
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/download-file')
def download_file():
# Path to the file you want to serve up
path_to_file = 'path/to/your/file.extension'
# Serve the file for download
return send_file(path_to_file, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment