Route static files for BottlePy web framework

I have been using BottlePy on my Raspberry Pi for quite some time. I really like the simplicity of the framework and the ease at which you can get something up and running. The one issue I had, was that you could not load static css or js files. 
There are ways around this, like placing your files in Gist and linking to the raw code, but this can be an issue when you want to run your webserver ‘offline’. The documentation is not very clear on how to do this, so here is what I did to get it working properly. 
This is the template that I use to route the static files. This is part of my main python code that runs the server.
Then all you need to do is add your file names in the .tpl file. By doing this, your css, or js files can be stored locally so they can still be served if you are offline. 
That is about it. I hope this helps because I really find this framework great to use and works flawlessly on my Raspberry Pi.
Greg

Raspberry PI: Bottlepy and Twitter Bootstrap

I have been using my Raspberry Pi for the last few months quite a lot. I am currently working on a temperature/environment monitor that logs temperature, using a DS18B20, and light conditions using an optical sensor. I also have two LED’s and two buttons to test basic input and output.
I am developing the application in python using the BottlePy framework. I chose the BottlePy framework as it is a single file that you add, it is very small and it works with Python 3. To control my GPIO’s, I am using the Quick2Wire library. To display the temperature data, I am using Google Charts.

This is a picture of the main temperature logging page so far.
However, the purpose of this post is to show how to integrate BottlePy and Bootstrap by Twitter. According to their website, Bootstrap is a “Sleek, intuitive, and powerful front-end framework for faster and easier web development.”
To display this, I am going to use my GPIO code which reads buttons and turns on LED’s through a webpage. 

To start off we need python file that will run as the server. Below is a snippet of code. The @route shows that this particular function will be run when /gpio us entered into the browser. The webpage is broken up into four sections. The first controls LED 1, the second controls LED 2, the third gets the state of both buttons and light sensor and then the fourth runs a .c file to flash the LED’s.
Now we need to have a look at the html code that creates the webpage. By default, Bootstrap works with the user downloading the .css and .js file. These are then statically linked to the html code. When using the BottlePy framework, I haven’t found a way to return a webpage template, AND a static file. Therefore I have found a link where the .css and .js files have been hosted. By using these links, you only have access to the default settings, but they are enough for what I am wanting to do.
You have to link to the .js and .css file and then by using certain classes that have been defined and explained on the Bootstrap website, you can get nice looking buttons, navigation bars, and tables. They have many options to use but these are the three that I used here for my site. 
When a button on the website is pressed, it sends the value back to the python function and the python code will perform the task. With the Flash LED button, the python code calls a .c file that has been compiled and runs when called. 
When requesting the button state, I return hex formatted colours to show the state of the buttons. These the colour in the table. 

The red shows that Button 1 has been pressed, the Green shows that Button 2 is not pressed and the Blue shows that the light level is currently light.
There are many different elements that can be added by using the Bootstrap framework. The documentation is really clear and easy to follow with many examples. 
If you get stuck or need some more explanations give me a shout.
Greg

Raspberry Pi: Using Bottlypy to control GPIO

I have been trying for a few days now to get control over the GPIO pins through a webserver. There are a number of tutorials on the web but having very little web programming experience, I struggled to understand and follow the tutorials. 
After working through a number of examples, and not getting what I want, I came across the bottlepy framework. The tutorial on their website and the todo example show enough ways on how to get data to an html page and how to get data from an HTML page.
I have been super busy at work and also trying to get it to work so haven’t had a chance yet to do a bit of an explanation on what I did.
To download the files I used click here.
You will need to edit your IP address in the index.py file.
Change to the directory on your Raspberry Pi and run $ python3 index.py
In your browser navigate to IPaddress:8080/index then follow the links. Have a look in index.py to see what pins I have used for the inputs and outputs.

Sorry this is so vague but as soon as I get a chance I will update this post with some more explanations.

Greg