Download multiple CSVs using Flask?

后端 未结 3 1658
青春惊慌失措
青春惊慌失措 2020-12-18 04:56

I have an app that takes in some information, performs some calculations using pandas, and turns the final pandas data frame into a CSV that is then downloaded using the Fla

3条回答
  •  生来不讨喜
    2020-12-18 05:01

    Building on @desfido's answer above, here would be some code implementation that does not involve using zip, and instead downloads two different files:

    from requests_toolbelt import MultipartEncoder
    
    def make_calculation(arg1, arg2):
        '''Does some calculations.
        input: arg1 - string, arg2- string
        puts results in two different dataframes
        and stores them in two different files,
        returns the names of those two files'''
        return filename1, filename2
    
    @app.route('test_app', methods=['GET', 'POST'])
    def test_app():
        form = Form1()
        if form.validate_on_submit():
            f1, f2 = make_calculation(str(form.input_1.data), str(form.input_2.data))
            m = MultipartEncoder({
               'field1': (f1, open(f1, 'rb'), 'text/plain'),
               'field2': (f2, open(f2, 'rb'), 'text/plain')
            })
            return Response(m.to_string(), mimetype=m.content_type)
       return render_template('test_app.html', form=form)
    

提交回复
热议问题