I am trying to create a form which downloads certain information upon submission via Flask. Here is a minimal working example.
app.py
from flask import Flask, render_template, request
app = Flask(__name__, template_folder='templates')
@app.route("/")
def home():
return render_template('index.html')
@app.route("/download", methods=["GET"])
def download():
return request.args
if __name__ == '__main__':
app.run()
templates/index.html
<form method="get" action="download" id='download-form'>
<select name="number">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit">
</form>
By submitting the form, the user is redirected to /download with page content of {"number" : "1"}. How do I instead download this without being redirected to a new page?
With ajax I can do something like:
$('#download-form').on('submit', function(e) {
e.preventDefault();
$.ajax({
type: 'GET',
url: '/dwonload',
data: $('#download-form').serialize(),
success: function(data) {
console.log(data); // how do I download this data?
},
});
});
But how do I then download the data (which is a string)?