下载烧瓶生成的html页面

Cha*_*ark 5 html python flask

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

Ret*_*old 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)
Run Code Online (Sandbox Code Playgroud)

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