So I am attempting to use bottle.py and twitter bootstrap together to make a small website. I need to be able to insert a reasonable amount of data at various points in the HTML using Python but I am not really sure I understand how the HTML and python are communicating.
Here is an example of twitter and bottle working together.
He mentions linking a couple of .js files in the html and I can see where he does that but I am not really sure how that affects how the python interacts with the html. Is there a callback from the Javascript that the python catches using request.GET.get().strip():
?
Also one of the last lines is:
return template('templates/gpio.tpl', colour1=colour1, colour2=colour2, colour3=colour3)
I am not sure how the templates/gpio.tpl
is connected to the html he mentions below. I understand that the colour# variables are referenced in the html (I assume this happens with the {{}} syntax) but I am not sure how the html gets called at all.
From what I understand (which so far isnt a whole lot) this is how it goes:
- User enters “server:port/gpio” into a webbrowser
- The python decorator gets called by bottle and the function is run returning the template at the bottom.
- This is where I get confused.
- A) How does the python script know to call the html?
- B) How does the gpio.tpl template code get sent to the html?
- C) Is it safe to assume that the python arguements sent to the template function can be referenced using the {{}} syntax or is there more to it?
- D) How does the html call back to the python to update the buttons he shows at the bottom?
- D.1) does the JS linked at the top have something to do with this?
Lastly: If anyone has a another/better example of linking bootstrap and bottle I would be very happy to see it.
This is quite a loaded post. Thank you for your patience. 😀
2
Answers
You’d really need to first learn how the HTTP protocol works… But let’s try to quickly answer your main question:
Quite simply: they don’t. What happens is:
routes
)Usually – but not necessarily – the HTTP response contains HTML content, generated by the controller using a template. IOW : bottle.py uses the template to generate html that is sent back to the client.
Once the response is sent, there’s no more “communication” until the client sends another request.
It depends on what you want to change…
For example, pushing the button could trigger a new HTTP request, you should then define a new feature in your code.
Let’s take the previous example, and imagine you want to add a switchOff button.
You have to add the following button somewhere in the gpio.tpl :
Then, modify the function gpio() to add a new condition with the following :