Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to download a generator with Python Flask?
    primarykey
    data
    text
    <p>I'm writing an application that generates large amounts of data that the end user will download (CSV files). I currently have a page that generates the data based on entry into a form and returns the created generator object to the user, like so:</p> <pre><code>@app.route('/data', methods = ['GET', 'POST']) def data(): form = Form() if form.validate_on_submit(): generator = create_generator(form) name = "results.csv" return Response(generator, mimetype="text/plain", headers={"Content-Disposition": "attachment;filename={}".format(name)}) return render_template('data.html', title = 'Data Page', form = form) </code></pre> <p>Now, what I would like to do is take that generator, along with some additional data, and pass it to another view/page in the application, so something like:</p> <pre><code>@app.route('/data', methods = ['GET', 'POST']) def data(): form = Form() if form.validate_on_submit(): generator, data = create_generator(form) name = "results.csv" #Can't send data in a redirect, but for example... redirect('/result', generator=generator, data=data, name=name) return render_template('data.html', title = 'Data Page', form = form) @app.route('/output', methods = ['GET', 'POST']) def output(): form = Form() return render_template('output.html', title = 'Output Page', generator=generator, data=data, name=name) </code></pre> <p>Where <code>output.html</code> would then have a clickable link to download the generator. In other words, the generator isn't returned immediately the way it is in the first example with <code>return Response()</code>, but instead waits for the user. </p> <p>To wrap up, the main two things I'm trying to do are:</p> <ol> <li>Pass a generator between pages </li> <li>Return a generator as a standard link (<code>&lt;a href={{generator}}&gt;&lt;/a&gt;</code> or something like that)</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload