I have tried reading the docs for Bottle, however, I am still unsure about how static file serving works. I have an index.tpl
file, and within it it has a css file attached to it, and it works. However, I was reading that Bottle does not automatically serve css files, which can't be true if the page loads correctly.
I have, however, run into speed issues when requesting the page. Is that because I didn't use the return static_file(params go here)
? If someone could clear up how they work, and how they are used when loading the page, it would be great.
Server code:
from Bottle import route,run,template,request,static_file @route('/') def home(): return template('Templates/index',name=request.environ.get('REMOTE_ADDR')) run(host='Work-PC',port=9999,debug=True)
Index:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <title>index</title> <link type="text/css" href="cssfiles/mainpagecss.css" rel="stylesheet"> </head> <body> <table style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="2" cellspacing="2"> <tbody> <tr> <td> <h1><span class="headertext"> <center>Network Website</center> </span></h1> </td> </tr> </tbody> </table> %if name!='none': <p align="right">signed in as: {{name}}</p> %else: pass %end <br> <table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2"> <tbody> <tr> <td> <table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2"> <tbody> <tr> <td style="width: 15%; vertical-align: top;"> <table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2"> <tbody> <tr> <td>Home<br> <span class="important">Teamspeak Download</span><br> <span class="important">Teamspeak Information</span></td> </tr> </tbody> </table> </td> <td style="vertical-align: top;"> <table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2"> <tbody> <tr> <td> <h1><span style="font-weight: bold;">Network Website</span></h1> To find all of the needed information relating to the network's social capabilities, please refer to the links in the side bar.</td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </body> </html>
Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library. Routing: Requests to function-call mapping with support for clean and dynamic URLs.
Flask – Static Files Usually, the web server is configured to serve them for you, but during the development, these files are served from static folder in your package or next to your module and it will be available at /static on the application. A special endpoint 'static' is used to generate URL for static files.
Static files in Flask have a special route. All application URLs that begin with "/static", by convention, are served from a folder located at "/static" inside your application's root folder.
Bottle framework learning checklistDownload Bottle or install via pip with pip install bottle on your local development machine. Work through the official Bottle tutorial. Start coding your Bottle app based on what you learned in the official tutorial plus reading open source example applications found above.
To serve static files using bottle
you'll need to use the provided static_file
function and add a few additional routes. The following routes direct the static file requests and ensure that only files with the correct file extension are accessed.
from bottle import get, static_file # Static Routes @get("/static/css/<filepath:re:.*\.css>") def css(filepath): return static_file(filepath, root="static/css") @get("/static/font/<filepath:re:.*\.(eot|otf|svg|ttf|woff|woff2?)>") def font(filepath): return static_file(filepath, root="static/font") @get("/static/img/<filepath:re:.*\.(jpg|png|gif|ico|svg)>") def img(filepath): return static_file(filepath, root="static/img") @get("/static/js/<filepath:re:.*\.js>") def js(filepath): return static_file(filepath, root="static/js")
Now in your html, you can reference a file like so:
<link type="text/css" href="/static/css/main.css" rel="stylesheet">
Directory layout:
`--static | `--css | `--fonts | `--img | `--js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With