Accessing Goaccess report.html within flask

114 views Asked by At

I have a website that uses flask and I would like to display the stats generated by goaccess from within my website.

in my route.py, I have added:

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

and I have made sure the report.html is in the template forlder.

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.

If there is no solutions, can anyone suggest a way to access report.html from the internet?

Thank you.

1

There are 1 answers

0
frainfreeze On

What you want to do is serve a static file using flask. See How to serve static files in Flask


If you take a look at the docs the render_template function "Renders a template from the template folder with the given context". What you have is a complete, self-contained HTML file, and not a template. You could instad use the 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 (e.g. 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."