Bottle web app not serving static css files

2024/9/16 23:16:56

My bottle web application is not serving my main.css file despite the fact I am using the static_file method.

app.py

from bottle import *
from xml.dom import minidom
@route('/')
def index():return template("index")@route('/glossaryXML')
def glossary():doc_def = minidom.parse("table_definitions.xml")terms = doc_def.getElementsByTagName("str_term")defins = doc_def.getElementsByTagName("str_definition")return template("list", terms=terms, defins=defins)@route('<filename>.css')
def stylesheets(filename):return static_file(filename, root='static')@error(404)
def fourofour(error):return "Error"run(host='localhost', port=8080, debug=True)

The page I am trying to access is the index page, in which index.tpl looks like

<!DOCTYPE HTML>
<html><head><title>ICT Applications Glossary</title><link type="text/css" href="main.css" rel="stylesheet"></head><body>It works</body>
</html>

My CSS file is located in a folder named "static" which is in my root folder

Answer

Instead specify your static route like this

@route('/<filename:path>')
def send_static(filename):return static_file(filename, root='static/')

This will serve any file in your static directory though not just css.

To make it stylesheet specific

@get('/<filename:re:.*\.css>')
def stylesheets(filename):return static_file(filename, root='static/')

Note: for the latter option you could put stylesheets in their own directory 'static/css' or just 'css' and keep them separate from other static resources (scripts, images etc.) to do this just specify the root parameter to be that directory e.g. `root='static/css'.

https://en.xdnf.cn/q/72722.html

Related Q&A

How to wrap text in OpenCV when I print it on an image and it exceeds the frame of the image?

I have a 1:1 ratio image and I want to make sure that if the text exceeds the frame of the image, it gets wrapped to the next line. How would I do it?I am thinking of doing an if-else block, where &qu…

pandas series filtering between values

If s is a pandas.Series, I know I can do this:b = s < 4or b = s > 0but I cant dob = 0 < s < 4orb = (0 < s) and (s < 4)What is the idiomatic pandas method for creating a boolean series…

python os.path.exists reports False when files is there

Hi have an application which is sometimes reporting that a file does not exist even when it does, I am using os.path.exists and the file is on a mounted network share. I am on OSX Yosemite, python 2.7.…

Python unhashable type: numpy.ndarray

I worked on making functions for K Nearest Neighbors. I have tested each function separately and they all work well. However whenever I put them together and run KNN_method, it shows unhashable type: n…

Efficient way to generate Lime explanations for full dataset

Am working on a binary classification problem with 1000 rows and 15 features. Currently am using Lime to explain the predictions of each instance. I use the below code to generate explanations for full…

how to handle javascript alerts in selenium using python

So I there is this button I want to click and if its the first time youve clicked it. A javascript alert popup will appear. Ive been using firebug and just cant find where that javascript is located an…

testing.postgresql command not found: initdb inside docker

Hi im trying to make a unittest with postgresql database that use sqlalchemy and alembicAlso im running it on docker postgresqlIm following the docs of testing.postgresql(docs) to set up a temporary po…

Recommended approach for loading CouchDB design documents in Python?

Im very new to couch, but Im trying to use it on a new Python project, and Id like to use python to write the design documents (views), also. Ive already configured Couch to use the couchpy view server…

Error when import matplotlib.pyplot as plt

I did not have any problem to use "plt", but it suddenly shows an error message and does not work, when I import it. Please see the below. >>> import matplotlib >>> import m…

Python NtQueryDirectoryFile (File information structure)

Ive written a simple (test) script to list files in a selected directory. Not using FindFirstFile; only native API. When I execute the script and watch, Win32API monitor tells me STATUS_SUCCESS. My Fil…