2

Variable rules isn't working as I'd expect when static_url_path is used.

Simply, why does this work:

import flask

app = flask.Flask(__name__)

@app.route("/", defaults={"p": "")
@app.route("/<path:p>")
def main(p):
    return "<h1>Hello %s</h1>" % p


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

But not this?

import flask

app = flask.Flask(__name__, static_url_path="")

@app.route("/", defaults={"p": "")
@app.route("/<path:p>")
def main(p):
    return "<h1>Hello %s</h1>" % p


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

Note the added static_url_path

Some background details; I'm new to both Flask and AngularJS, but are using them here in conjunction with each other and for those not familiar with AngularJS; I'm using AngularJS to produce a single-page application in which paths are used for dynamically replacing content within a document, as opposed to reloading the whole page. Thus, I need all paths routed to the same html document (not included in the above example), ideally coming form the same function, so as to let AngularJS handle rendering as opposed to Flask.

The below answer solved that issue, but it isn't working when status_url_path is used, and I can't figure out why.

Flask route for AngularJS with HTML5 URL mode

Adding this for reference, as he is doing this exact thing.

http://www.youtube.com/watch?v=2geC50roans

1 Answer 1

2

You are effectively telling Flask to map /<path:static_path> to static files. That means that everything is now considered static. Don't do this!

Angular pages don't need to call into Flask static routes; keep your static routes under /static, have / produce your Angular application, then use other routes to handle Angular AJAX calls. These can have proper paths; Angular doesn't dictate what routes your Flask server will respond to.

Then keep your static route to serve the JavaScript and CSS and images, e.g. the actual static content referenced from your HTML page.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Martijn, I'll try and wrap my head around this and get back to you.
Seems to work fine. To be clear, this also means that all addresses, including those to AngularJS controllers, must include the flask "static/" directory? E.g. templateUrl: "static/views/list.html"
@MarcusOttosson: yes, if those parts are static (e.g. the Flask server only has to produce the file data, not insert anything in a template or produce other dynamic info).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.