Amriteya
Amriteya

Reputation: 1172

Flask unable to resolve static files

I am trying to write a simple API using Flask. I am following Miguel Grinberg's blog.

I have a use-case where I am trying to serve a static page. I am unable to serve the static webpage.

Here is my app.py

from flask import Flask, request, abort, render_template

app = Flask(__name__)

@app.route('/sample/application/', methods=['GET'])
def static_website():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

And following is my directory layout:

flask_application/
    - app.py
    - static/
        - index.html
        - cs/
            - app.css
            - bootstrap.css
        - js/
            - jquery.js
        - images/
            - logo.png

Before anyone marks this as a duplicate, I have gone through the following posts and it has not resolved my issue.

There are other questions but no ones gives a concrete solution to resolve css, js files in static pages.

The server created by Flask is unable to pickup the js, css files.

Also, I am avoiding templating because this the only use-case I have and I am pretty sure, I am not going to add any other pages or make it more complex.

Upvotes: 1

Views: 1618

Answers (2)

mphuie
mphuie

Reputation: 960

If you don't care about renderung a template, you can just send the static file on your route. This way you can just leave everything in the static directory.

app.send_static_file('index.html')

Just remember the rest of the assets will need to be referenced as /static/<asset>, ie <script src="/static/js/jquery.js></script>

Upvotes: -1

Jonathan
Jonathan

Reputation: 8890

The template needs to be in a folder called templates. So the correct solution should be

flask_application/
   - templates/
     - index.html 

You can read the documentation for the render_template method.

Upvotes: 2

Related Questions