下载烧瓶生成的html页面

时间:2015-10-30 16:12:23

标签: python html flask

我想在烧瓶生成的网页上放一个按钮,让用户在用户点击按钮时将html页面下载为文件。我想象的是将渲染的html保存到BytesIO并通过send_file发送,但我无法找到如何将渲染的页面保存到文件对象中。我怎么能这样做?

1 个答案:

答案 0 :(得分:5)

您可以尝试这样的事情:

import StringIO
from flask import Flask, send_file, render_template

def page_code():   
    strIO = StringIO.StringIO()
    strIO.write(render_template('hello.html', name='World'))
    strIO.seek(0)
    return send_file(strIO,
                     attachment_filename="testing.txt",
                     as_attachment=True)

它没有经过测试,但应该给你一个想法。