× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

tips in integrating flask and angular, routing

Kyle J. Roux
Jul 18, 2015
<p>when creating routes on your backend using flask, if you are using angular, you are going to find yourself putting templates for your angular app closer to the javascript. or at least away from the reagular templates that will be processed with jinja, (which we could integrate but jinja is very slow compared to angular by itself). When doing this you need to intercept the requests for the templates from angular to flask, so you can inform flask where they live, so it can properley send them back to the user. </p> <p>to do that make all of your angular template files with a standard extension, i just use the normal `.html`, then create a route that intercepts any requests for files with that extension:</p> <pre> @app.route('/&lt;path:template&gt;.html') def send_template(template): </pre> <p>then use your knowledge of where the templates are to construct the file correct file name</p> <pre> template_file = '{}.html'.format(os.path.join(TEMPLATE_DIR,template)) </pre> <p>where TEMPLATE_DIR, is the location of your angular templates</p> <p>then send the file to the user</p> <pre> return flask.send_file(template_file) </pre> <p>and angular should correctly receive any templates it might ask for</p>
comments powered by Disqus