skip to Main Content

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


  1. You’d really need to first learn how the HTTP protocol works… But let’s try to quickly answer your main question:

    how the HTML and python are communicating

    Quite simply: they don’t. What happens is:

    1. your client (usually your browser) send an HTTP request to your site
    2. the front web server (Apache, Nginx, whatever) sends this request to the bottle.py application
    3. the bottle.py app dispatch the request to the right controller function matching the url’s path portion and request’s method against the defined routes)
    4. the controller function does what it has to do and returns an HTTP response to the front web server
    5. the front web server send this response to your client

    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.

    Login or Signup to reply.
  2. So if I wanted to make an html button that changed something on the page how would I send that response back to bottle.py regenerate the page with the change?

    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 :

    <input type="submit" class="btn" name="LedsOff" value="Turn off the leds!">
    

    Then, modify the function gpio() to add a new condition with the following :

    elif request.GET.get('LedsOff','').strip():
        from quick2wire.gpio import Pin, exported
        with exported(Pin(12, Pin.Out)) as out1, exported(Pin(13, Pin.Out)) as out2:
            out1.value = 0
            out2.value = 0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search