设置Flask的iframe src响应

编程入门 行业动态 更新时间:2024-10-26 23:28:12
本文介绍了设置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响应

本文发布于:2023-10-11 17:43:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Flask   iframe   src

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!