0
votes

I am implementing a web app by Flask framework. This web app is about to server static html pages whose structure is like java doc.

codes:

from flask import Flask, send_from_directory,url_for

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

@app.route("/report")
def view_report():
    url_for('static', filename='report/flexmonkey/html/')
    return send_from_directory('static', 'report/flexmonkey/html/index.html')

if __name__ == "__main__":
    app.run(debug=True)

the index.html:

<html xmlns:lxslt="http://xml.apache.org/xslt" xmlns:stringutils="xalan://org.apache.tools.ant.util.StringUtils">
<head>
<META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Unit Test Results.</title>
</head>
<frameset cols="20%,80%">
<frameset rows="30%,70%">
<frame src="overview-frame.html" name="packageListFrame">
<frame src="allclasses-frame.html" name="classListFrame">
</frameset>
<frame src="overview-summary.html" name="classFrame">
<noframes>
<h2>Frame Alert</h2>
<p>
                This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
            </p>
</noframes>
</frameset>
</html>

the directory structure:

~/workspace/testReport/static/report/flexmonkey/html $ ls
allclasses-frame.html  alltests-fails.html  automation  overview-frame.html    stylesheet.css
alltests-errors.html   all-tests.html       index.html  overview-summary.html

error message:

127.0.0.1 - - [13/Sep/2012 11:01:25] "GET /overview-frame.html HTTP/1.1" 404 -
127.0.0.1 - - [13/Sep/2012 11:01:25] "GET /allclasses-frame.html HTTP/1.1" 404 -
127.0.0.1 - - [13/Sep/2012 11:01:25] "GET /overview-summary.html HTTP/1.1" 404 -
2
I would suggest actually adding a question so folks know what you're looking for help with. Presumably, you want someone to tell you why you're getting those errors, but that's not immediately clear. Have you tried using different directories in send_from_directory? Seems like maybe the issue is just that the path you're giving isn't quite right. Maybe it needs to be absolute? - aezell
Armin Ronacher says "Relative paths to send_from_directory are relative to the application root folder". flask.pocoo.org/mailinglist/archive/2011/9/26/… - Geordee Naliyath

2 Answers

4
votes

You don't need to use Flask if all you want to do is serve static files. From the directory where you have the html files, type python -m SimpleHTTPServer then browse http://localhost:8000/

For Python 3, type python -m http.server instead.

0
votes

by default flask will serve anything in /static folder as static files, unless redefined as app = Flask(__name__, static_url_path='some_other_folder'). So put all your html files under static folder and run a simple flask server:

from flask import Flask, send_from_directory

app = Flask(__name__)

@app.route('/')
def root():
    return send_from_directory('','index.html') # serve root index.html

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)

then visit http://localhost:8080/static/report/flexmonkey/html/overview-summary.html and you should get it.