简体   繁体   English

访问 Goaccess 报告。flask 内的 html

[英]Accessing Goaccess report.html within flask

I have a website that uses flask and I would like to display the stats generated by goaccess from within my website.我有一个使用 flask 的网站,我想在我的网站中显示 goaccess 生成的统计信息。

in my route.py, I have added:在我的 route.py 中,我添加了:

@main.route("/report")
def report():
    return render_template('report.html')

and I have made sure the report.html is in the template forlder.并且我已确保 report.html 在模板中。

the problem is that I get the following error:问题是我收到以下错误:

File "/templates/report.html", line 866, in template
    <div class="grid-module {{#className}}{{className}}{{/className}}{{^className}}gray{{/className}}">
File "/lib/python3.6/site-packages/jinja2/environment.py", line 497, in _parse
    return Parser(self, source, name, encode_filename(filename)).parse()
File "/lib/python3.6/site-packages/jinja2/parser.py", line 901, in parse
    result = nodes.Template(self.subparse(), lineno=1)
File "/lib/python3.6/site-packages/jinja2/parser.py", line 874, in subparse
    next(self.stream)
File "/lib/python3.6/site-packages/jinja2/lexer.py", line 359, in __next__
    self.current = next(self._iter)
File "/lib/python3.6/site-packages/jinja2/lexer.py", line 562, in wrap
    for lineno, token, value in stream:
File "/lib/python3.6/site-packages/jinja2/lexer.py", line 739, in tokeniter
name, filename)
jinja2.exceptions.TemplateSyntaxError: unexpected char '#' at 298804

Would anyone know how to solve this?有人知道如何解决这个问题吗? I don't want to touch report.html and would like to use it as such.我不想碰 report.html 并想这样使用它。

If there is no solutions, can anyone suggest a way to access report.html from the internet?如果没有解决方案,谁能建议一种从互联网上访问 report.html 的方法?

Thank you.谢谢你。

What you want to do is serve a static file using flask.您要做的是使用 flask 提供 static 文件。 See How to serve static files in Flask请参阅如何在 Flask 中提供 static 文件


If you take a look at the docs the render_template function "Renders a template from the template folder with the given context".如果您查看文档render_template function “使用给定上下文从模板文件夹渲染模板”。 What you have is a complete, self-contained HTML file, and not a template.您拥有的是一个完整的、独立的 HTML 文件,而不是模板。 You could instad use the send_from_directory function.您可以使用send_from_directory function。 Do note that in production environment you probably do not want to serve file using Flask but instead use the web server (eg NGINX).请注意,在生产环境中,您可能不想使用 Flask 提供文件,而是使用 web 服务器(例如 NGINX)。

Example code:示例代码:

from pathlib import Path
import os
...    

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
TEMPLATES_DIR = os.path.join(APP_ROOT, 'templates')

@app.route("/report")
def report():
    REPORT_FILE = os.path.join(TEMPLATES_DIR, "report.html")
    if Path(REPORT_FILE).is_file():
        return send_from_directory(REPORT_FILE_PATH, "report.html")
    return "No stats."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM