我正在开发一个需要生成报告的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响应
发布评论