0

The file index.html is static, and hence it would be wasteful to pass it through render_template(). If I store index.html under static/ and use app = Flask(__name__), all is well.

But if I point static_url_path='' to the root and keep index.html in the same folder as the Python app, I get

127.0.0.1 - - [21/Nov/2016 14:35:54] "GET / HTTP/1.1" 404 -

index.html

<!DOCTYPE html>
<head></head>
<body>
    <h2>Hello, World!</h2>
</body>

.py

from flask import Flask, current_app
app = Flask(__name__, static_url_path='')

@app.route('/')
def hello_world():
    return current_app.send_static_file('index.html')

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

How do I use a fully flat directory hierarchy, keeping the two files above in the same directory?

Community
  • 1
  • 1
Calaf
  • 10,113
  • 15
  • 57
  • 120
  • I would discourage you from serving static pages through Flask if that's all you need to do. Look into `static_folder`, instead of `static_url_path`. See [this post](http://stackoverflow.com/a/20648053/3261863) for `send_from_directory`, which may be of use to you, as you don't need to clobber your static settings to serve one-off pages. – Chris Beard Nov 22 '16 at 17:33

2 Answers2

0

I think you need to set static_folder rather than status_url_folder and specify the absolute path of the folder rather than just a blank string.

You can view the default static folder path with print(app.static_folder)

scotty3785
  • 6,763
  • 1
  • 25
  • 35
0

You could just use flask.send_file():

import flask

app = flask.Flask(__name__)


@app.route('/')
def hello_world():
    return flask.send_file('index.html')

if __name__ == '__main__':
    app.run()
William Jackson
  • 1,130
  • 10
  • 24