Python 下载生成的html页面
我想在flask生成的网页上放置一个按钮,让用户在单击按钮时将html页面作为文件下载。我想象的是将呈现的html保存到Python 下载生成的html页面,python,html,flask,Python,Html,Flask,我想在flask生成的网页上放置一个按钮,让用户在单击按钮时将html页面作为文件下载。我想象的是将呈现的html保存到BytesIO并通过send\u file发送,但我找不到如何将呈现的页面保存到文件对象中。我该怎么做呢?您可以尝试以下方法: import StringIO from flask import Flask, send_file, render_template def page_code(): strIO = StringIO.StringIO() s
BytesIO
并通过send\u file
发送,但我找不到如何将呈现的页面保存到文件对象中。我该怎么做呢?您可以尝试以下方法:
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)
它没有经过测试,但应该会给你一个想法。非常感谢!我没想到
render\u template
会以字符串形式返回整个html页面,我认为它在幕后会做得更多,因为API文档没有说明其返回值,或者我只是错过了它。这就解决了问题。