最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Flask and Angular Web Application Routing - Stack Overflow

programmeradmin1浏览0评论

I want to make a web app primarily using Python Flask framework as well as AngularJS and whilst thinking about doing so and maintaining features of the application I started wondering how the routing would be handled.

In flask it is very easy to manage how the back end directly deals with routing:

@app.route('/')
def home():

In Angular it is easy to manage routing as well obviously, and the data can be worked with by loading specific controllers in to access certain parts of the back end via XHR and what not.

To make a good single page web app would I benefit most from delegating all the routing responsibility to either Flask or Angular, or would it make sense to let Flask handle all the routes with just a /, and let Angular deal with all the hash-routing for changes in that single view.

I'm not exactly sure how to tackle and delegate this and I realize there is no one way to do it but I interested to see if anyone has messed with this before and what they found to be easier to work with.

I want to make a web app primarily using Python Flask framework as well as AngularJS and whilst thinking about doing so and maintaining features of the application I started wondering how the routing would be handled.

In flask it is very easy to manage how the back end directly deals with routing:

@app.route('/')
def home():

In Angular it is easy to manage routing as well obviously, and the data can be worked with by loading specific controllers in to access certain parts of the back end via XHR and what not.

To make a good single page web app would I benefit most from delegating all the routing responsibility to either Flask or Angular, or would it make sense to let Flask handle all the routes with just a /, and let Angular deal with all the hash-routing for changes in that single view.

I'm not exactly sure how to tackle and delegate this and I realize there is no one way to do it but I interested to see if anyone has messed with this before and what they found to be easier to work with.

Share Improve this question asked Jan 7, 2015 at 17:58 Dominic FarolinoDominic Farolino 1,3831 gold badge22 silver badges41 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Take a look at this example, it's extremely well-written and documented. If you look in the controllers.py file you'll notice that he uses Flask/Python for the RESTful API and he lets Angular handle all the static templates.

Edit: I've never looked at this before, but this seems to be really cool and might make your life easier. Also, here's a question that's extremely similar to yours which I just found.

I know this is old thread. But If anyone wants to serve Angular or AngularJS by Flask then the easiest solution is to handle 404 not found error by Angular routing and return client side page on flask side.

@app.errorhandler(404)
def not_found_error(error):
    return render_template('index.html')

It depends on how you want to structure your app I suppose. To me it makes sense, if you consider your entire website one app, to allow Angular to handle all the client endpoints, and use Python as a RESTful service.

However, if you have any static content that you want to render server side outside of your SPA, then you could do that routing in Python.

One option is to separate the Flask and AngularJS application. This way, Flask routing is used for exposing api endpoints that the client side app (AngularJS) will use. And AngularJS routing will be used for client-side routes.

I've written a tutorial on using Flask and AngularJS together - http://tutsbucket./tutorials/building-a-blog-using-flask-and-angularjs-part-1/

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论