Angular 4 frontend with python flask backend how to render simple index page -


hello python community angular , node.js developer , want try python backend of server because new python want ask how target dist folder contains html , css , js files angular 4 apps in flask python server

because app spa application have set routes inside angular routing component

when run or other route string message './dist/index.html' , know return string message want tell flask whatever route user type on url let angular render page because inside angular app have set pages , work

any how start flask , angular build simple rest api

now have file structure

python-angular4-app                   |___ dist                   |      |___ index.html                   |      |___ style.css                   |      |___ inline.js                   |      |___ polyfill.js                   |      |___ vendor.js                   |      |___ favicon.ico                   |      |___ assets                   |                   |___ server.py 

my server.py have content

from flask import flask  app = flask(__name__, )  @app.route('/') def main():     return './dist/index.html'   @app.route('/about') def about():     return './dist/index.html'   @app.route('/contact') def contact():     return './dist/index.html'  if __name__ == "__main__":     app.run(debug=true) 

best regards george35mk thnx help

i don't think it's possible access angular 'dist' directory via rest api. routing should done on client-side angular, , flask should handle end-points.

in terms of building rest api, i'd recommend this:

from flask import flask, jsonify  app = flask(__name__)  tasks = [     {         'id': 1,         'title': u'buy groceries',         'description': u'milk, cheese, pizza, fruit, tylenol',          'done': false     },     {         'id': 2,         'title': u'learn python',         'description': u'need find python tutorial on web',          'done': false     } ]  @app.route('/todo/api/v1.0/tasks', methods=['get']) def get_tasks():     return jsonify({'tasks': tasks})  if __name__ == '__main__':     app.run(debug=true) 

this helpful tutorial on building basic rest api in flask.

this plug in nicely client-side in angular:

getinfo() {  return  this.http.get(    'http://myapi/id')    .map((res: response) => res.json()); 

}


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -