Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure a flask with an angular.js

How to structure flask app with angular.js front-end? What is best practice? Should I use webserver like ngnix to host static files even when I'm working on development?

With flask default, I can serve index.html like below:

@app.route('/')
def index():
    return make_response(open('static/index.html').read())

or

@app.route('/')
def index():
    return send_from_directory('static', 'index.html')

But, there is a problem where I cannot point js files without '../static' prefix. I just want to point angular.js and all others like:

<script src="lib/angular/angular.js"></script>

not

<script src="../static/lib/angular/angular.js"></script>

Can I change all static file prefix in flask? Or is there a good way to solve this problem?

Thanks.

like image 370
ccoroom Avatar asked Apr 19 '13 08:04

ccoroom


People also ask

Can we use Angular with Flask?

Welcome back. With the Redis task queue setup, let's use AngularJS to poll the back-end to see if the task is complete and then update the DOM once the data is made available.

Can we use AngularJS with Python?

You will use Angular to implement the user interface features and Python for the backend. These days it is not uncommon to have an API that is responsible not only for persisting data to the database, but also dealing with business requirements like permissions, data flow, data visibility, and so on.

Is Angular better than Flask?

AngularJS has a broader approval, being mentioned in 2793 company stacks & 1806 developers stacks; compared to Flask, which is listed in 502 company stacks and 509 developer stacks.


1 Answers

If you really want to, you can:

app = Flask(__name__, static_url_path='')

Although I'd just use the absolute URL:

/static/lib/angular/angular.js
like image 54
Blender Avatar answered Oct 08 '22 13:10

Blender