1

I created an Angular website with ui-router:

angular app structure
|--index.html
|--js
   |--app.js
   |--angular.js 
   |-- ...
|--stylesheets
   |--main.css
   |-- ...
|--template 
   |--navbar.html
   |--about.html
   |-- ...

Each js and css is linked like this:

<script src="js/main.js"></script>

I want to serve this with Flask. I threw everything in the "templates" folder and wrote a simple Flask app:

server.py:

from flask import Flask, make_response

app = Flask(__name__)

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

app.debug = True

if __name__ == '__main__':
    app.run()
flask app
|--server.py
|--templates
   |--index.html
   |--js
      |--app.js
      |--angular.js 
      |-- ...
   |--stylesheets
      |--main.css
      |-- ...
   |--template 
      |--navbar.html
      |--about.html
      |-- ...

None of my file are loading when I go to the root url. How do I serve the Angular files from the Flask app?

2 Answers 2

2

You need to render your template. The best way to do that is

@app.route('/')
def view():
    return render_template('index.html')
Sign up to request clarification or add additional context in comments.

1 Comment

Well, I also forgot this. But maybe we need merge our answers? :)
1

First, move your all of the .js file and the .css file into a static directory and do something like:

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">

Put this in your templates before </head>, it will loads static/main.css.

And this will loads the static/app.js:

<script type="text/javascript" src="{{ url_for('static', filename='app.js') }}"></script>

About why, here is the document.

2 Comments

stylesheets won't work here. url_for takes the name of an endpoint, not a folder.
@dirn Oops, let me fix it. Thanks :)

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.