how to make downloadable file in flask
To make a downloadable file in Flask, follow these steps:
- First, import the necessary modules:
from flask import Flask, send_file
- Create a Flask application:
app = Flask(__name__)
- Define a route to handle the file download request:
@app.route('/download', methods=['GET'])
def download_file():
path = "path_to_your_file/file_name.extension" # Replace with the actual path and file name
return send_file(path, as_attachment=True)
- Run the Flask application:
if __name__ == '__main__':
app.run()
Make sure to replace "path_to_your_file" with the actual file path and "file_name.extension" with the actual file name and extension. This code sets up a route '/download' to handle the file download request, and the send_file
function is used to send the file as an attachment. When the route is accessed, the file specified by the path will be downloaded by the user.