设置Flask的iframe src响应 [英] Set iframe src response from Flask

查看:1290
本文介绍了设置Flask的iframe src响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个需要生成报告的web应用程序(一些自动生成的html文件,如junit报告) 。

在report_dispay页面上,我有一个导航栏,左边有多个报表标题(html链接)。在右侧,有一个iframe,当单击一个链接时,报告将显示在其中。报告生成后,我发送URI(相对文件位置,即reports / html / index)。但是当我设置iframe的src属性时,在命令行中输出404,找不到reports / html / index。

你有什么想法如何注册生成的报告到应用程序?



非常感谢,
Vycon

解决您需要为这些报告注册一个处理程序:

 #此处的其他导入
from werkzeug.utils import safe_join

#... snip Flask setup ...

@ app.route(/ reports /< path:report_name> )
def report_viewer(report_name):
如果report_name是None:
abort(404)

BASE_PATH =/ your / base / path / to / reports
$ b fp = safe_join(BASE_PATH,report_name)
打开(fp,r)作为fo:
file_contents = fo.read()

返回file_contents


Currently, I got a question about Flask application.

I am developing a web app that need to generate reports (some auto-generated html files like junit report).

On the report_dispay page, I have a navigation bar on the left on which there are multiple report titles (html links); and on the right side, there is iframe inside which the report will be displayed when one link is clicked. After the report is generated, I send back the URI (relative file location, i.e., "reports/html/index"). But when I set the src attribute of iframe, the flask command line print 404, cannot found "reports/html/index".

Do you have any idea how to "register" the generated reports to the application?

Thanks so much, Vycon

解决方案

You'll need to register a handler for those reports:

# Other imports here
from werkzeug.utils import safe_join

# ... snip Flask setup ...

@app.route("/reports/<path:report_name>")
def report_viewer(report_name):
    if report_name is None:
        abort(404)

    BASE_PATH = "/your/base/path/to/reports"

    fp = safe_join(BASE_PATH, report_name)
    with open(fp, "r") as fo:
        file_contents = fo.read()

    return file_contents

这篇关于设置Flask的iframe src响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆