1

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

vycon
  • 139
  • 2
  • 12

1 Answers1

2

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
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • @vycon - nope - the `path` converter will accept any number of segments - so this route will match `reports/index`, `reports/some_name`, `reports/some/folders/and/another/name`, etc. – Sean Vieira Aug 03 '12 at 20:13
  • Thanks for your help, Sean! I have another question about this. My report consists of several static html file like java doc. Say, the index.html will be displayed. When the link in index.html is clicked, it will forward iframe page to other static html page. I have no control of these static html pages since they are automatically generated. How do I register handlers for these related reports? Thanks. – vycon Aug 03 '12 at 20:23
  • Thanks, Sean! Instead of sending back the file contents, how can I send the report path back to set the iframe's src attribute? I just tried with your scripts. I can get the file content now, but it seems that the report path is not registered. When the javascript script try to set the src attribute, it gave 404 error – vycon Aug 03 '12 at 21:03
  • @vycon - if you are running Flask at the root of your domain it is handling *all* the requests. You'll have to send back the contents or else store the HTML in the `static` folder of your application. Otherwise, the problem is with the parent server. – Sean Vieira Aug 03 '12 at 21:41
  • If I just copied the generated report folder into static folder. But it still doesnt work. Sorry, I am pretty new to Flask. – vycon Aug 03 '12 at 21:59