vycon
vycon

Reputation: 139

Set iframe src response from Flask

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

Upvotes: 1

Views: 2152

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 160015

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

Upvotes: 2

Related Questions