Question

So I have a bottle web framework running but I would like to have one webpage in it.

I have already created the webpage in html and css but I'm not sure how to make bottle use it. I have it displaying just the html but the css part of it does not work.

I've tried googling around but I can't seem to find an example of this.

@get('/test')
def test():
    return static_file('index.html' , root="views")

My css files are in the same directory as the views folder.

Was it helpful?

Solution

from bottle import static_file


@route('/static/<filename>')
def server_static(filename):
    return static_file(filename, root='/path/to/your/static/files')

This is the code that the Bottle docs give for serving a static file.

OTHER TIPS

In case we have different folders for js and css files (being used in html files) which is general case in all projects, we need to explicitly serve js and css directory contents separately.

Refer below code for further details:

from bottle import route
from bottle import static_file

#Hosts html file which will be invoked from browser.
@route('/filesPath/<staticFile>')
def serve_static_file(staticFile):
    filePath = '/path/to/your/static/file/'
    return static_file(staticFile, filePath)

#host css files which will be invoked implicitly by your html files.
@route('/files_path/css/<cssFile>')
def serve_css_files(cssFile):
    filePath = '/path/to/your/css/file/'
    return static_file(cssFile, filePath)

# host js files which will be invoked implicitly by your html files.
@route('/files_path/js/<jsFile>')
def serve_js_files(jsFile):
    filePath = '/path/to/your/jss/file/'
    return static_file(jsFile, filePath)

Bottle is best suited for APIs, where routes don't return HTML files. However, you can serve static HTML files with the static_file function.

If you're looking for a more comprehensive framework with HTML templating, try Flask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top