0

I'm using flask to serve a website rendered via sphinx. The problem is I can't get it to display my css and js properly as it is looking in the wrong places.

Folder structure

Container
├───server.py
├── html
│   ├── index.html
│   ├── _static
│   │   ├── css
│   │   │   ├── theme.css

index.html (excerpt)

<link rel="stylesheet" href="_static/css/custom.css" type="text/css" />

I can't change this as it is dynamically generated by sphinx.

server.py

from flask import Flask, redirect, url_for, render_template

app = Flask(__name__, root_path='html/', template_folder='', static_url_path='', static_folder='_static')

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

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

Error:

127.0.0.1 - - [22/Jan/2022 17:41:08] "GET /_static/css/custom.css HTTP/1.1" 404 -

So I have considered this topic: How to serve static files in Flask:

  • set static path to root directly (static_url_path='')
  • set static folder to _static (which is directly under root)
  • set template folder to root folder (template_dir='')

Other than that I made html/ my root dir. But its still not working. What else do I have to do to make it work?

glades
  • 1,202
  • 4
  • 17

0 Answers0