Search code examples
pythonurl-routingbottlestatic-filesinitializr

initializr with bottlepy - css not loaded


I'm trying to use a boilerplate I downloaded from Initializr together with bottle.py. I'm obviously doing something wrong since when I simply try to load index.html the site renders without using any of the stylesheets and I get the following errors in the browser console:

Use of getUserData() or setUserData() is deprecated.  Use WeakMap or element.dataset instead. requestNotifier.js:52
The stylesheet http://localhost:8080/css/normalize.min.css was not loaded because its MIME type, "text/html", is not "text/css". localhost:8080
The stylesheet http://localhost:8080/css/main.css was not loaded because its MIME type, "text/html", is not "text/css". localhost:8080
SyntaxError: syntax error modernizr-2.6.2-respond-1.1.0.min.js:1
SyntaxError: syntax error plugins.js:1
SyntaxError: syntax error

My app looks like this:

import bottle  # Web server
from bottle import run, route, static_file, error, template  # import request


@route('/')
def index():
    return static_file('index.html', root='./html/')

@route('./css/<filename>')
def server__static(filename):
    return static_file(filename, root='./css/')

if __name__ == '__main__':
    # To run the server, type-in $ python server.py
    bottle.debug(True)  # display traceback
    run(host='localhost', port=8080, reloader=True)

The stylesheets are called from index.html like so:

    <link type="text/css" rel="stylesheet" href="css/normalize.min.css">
    <link type="text/css" rel="stylesheet" href="css/main.css">

My basic folder structure is (snipped js folder etc.):

bottle_test.py
- html
      index.html
      - css
           main.css
           normalize.min.css

I understand that bottle doesn't serve static files by itself and I did play around with the route to server-static and added, changed and removed the mimetype argument but can't get it to work.

The entire boilerplate sits in the html folder and the app does find index.html. I tried this in Firefox and Chrome. I'm on Win 8.1 and anaconda's python 2.7 if that matters. What am I doing wrong?


Solution

  • Your paths seem wrong. The CSS route shouldn't start with a ., seems it interferes with how Bottle parses it. And your CSS static_file root doesn't properly reflect the css folder placement relative to the working directory.

    This should work alright:

    @route('/css/<filename>')
    def server__static(filename):
        return static_file(filename, root='./html/css')