python - 通过 pyramid Response 呈现动态生成的 HTML

标签 python pyramid python-2.6 python-2.7 python-2.x

我是 python Pyramid 框架的新手,请帮助我。

我有一个动态生成的 HTML。此 HTML 由 python 脚本生成,该脚本动态写入(标签/表格),这些脚本是从一些 'xyz.html' [使用 beautifulsoup] 提取到另一个 'abc.html'。

现在我需要将此 html 页面 ('abc.html') 作为 'pyramid.response' 的 'Response' 对象发回。

我该怎么做。我尝试了以下

    _resp = Response()
    _resp.headerlist =  [('Content-type',"text/html; charset=UTF-8'"\]
    _resp.app_iter = open('abc.html','r')
    return _resp

还有

   with open('abc.html','r') as f:
      data = f.read()
      f.close()
   return Response(data,content_type='text/html')

两者均无效。

PS:我不能使用 renderer="package:subpack/abc.html"或任何类似的渲染器,因为这个生成的 html 每次都存储在动态生成的位置,所以我无法猜测这个 html 文件的最终存储位置。

在此先感谢您的帮助。

最佳答案

我有点惊讶你的第一个例子不起作用。从 Pyramid 文档中查看此食谱条目,看看是否有帮助。

http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/static_assets/files.html#serving-file-content-dynamically

关于python - 通过 pyramid Response 呈现动态生成的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7981402/

相关文章:

python - pyqtgraph 中填充图的透明度

python - 无法在数据框中以自定义方式存储输出

Python:从列表中删除空格和 '\n'

python - 尝试使用 uWSGI 部署 python Pyramid 应用程序

python - 删除 XML 字符串中的空格

python - python2.6中没有索引的字符串格式化

python - 如何在 VSCode Jupyter Notebook 中拆分单元格?

pyramid - 未显示“gunicorn --paster”日志消息

python - 如何使用 Beaker 将值保存到 Pyramid 中基于匿名 cookie 的 session

Python:如何使用 main() 中定义的变量,和/或使用包含两个类的列表而不会出错?