Tutorials

Choosing a web framework

The easiest way to get the Python api running is to use a web framework.

The best one I know for Python is cherrypy. So, let's start this tutorial using cherrypy:

  • Download and install CherryPy
  • Download and unpack the Python API into your site-packages folder

A tutorial for using rialtoPython with TurboGears can be found here (Thanks to Flávio Coelho)

First of all a CherryPy Page

  • Just to test, if the CherryPy webframework is running on your machine, try the following:
import cherrypy
 
class Window:
 
    def index(self):
        out = '<html><head><title>CherryPy</title></head><body><h1>Hello World</h1></body></html>'
        return out
 
    index.exposed = True
 
cherrypy.root = Window()
 
if __name__ == '__main__':
    cherrypy.server.start()
  • Save the script and start it
  • point your browser to http://localhost:8080
  • You should see something like “Hello World” in your browser window.

The first Rialto window

  • Take the code from the CherryPy example above and add an import statement for the RialtoPython API.
from rialtoPython import *
  • In the index method, define some variables, that we will need for our rialto page, after killing the old contents of the method:
DIV                 = "document.body"
LAYOUT              = "ihmDemo"
WINDOW              = "myWindow"
WINDOW_TITLE        = "My First Pythonic Rialto Window"
FRAME               = "boxDemo"
FORM                = "myForm"
URL                 = "do"
  • Now instantiate the RialtoPython-class and initialize a list as container for the output:
rp = rialtoPython.RialtoPython()
out = []
  • Now create the page. Begin with the site template containing all statements to link the JS and CSS into the page, add a script-tag and set the language:
out.append(rp.addImport(devMode=False))
out.append('<script>')
out.append('\trialto.I18N.setLanguage("en");\n')
  • Now open up a new layout with a window on it:
out.append(rp.openLayout(LAYOUT))
out.append(rp.windowTag(WINDOW, WINDOW_TITLE, DIV))
  • So, now we would have a Window with nothing on it. That's boring ;-)
  • Let's add a Frame containing a label, a textfield and, let's say, two buttons.
out.append(rp.frameTag(FRAME, '30', '30', '300', '120', 'My 1st python Box', 'true', 'false', 'relative', 'false', WINDOW))
out.append(rp.formTag(FORM,URL,FRAME))
out.append(rp.labelTag('label1', '25', '10', 'Spam', FORM))
out.append(rp.textTag('login', '25', '90', '200', 'A', FORM, 'true', 'true', 'Eggs'))
 
out.append(rp.buttonTag('doLogon', 'Ni!', '70', '10', 'And now to something completely different', ['FORM', FORM], FORM))
out.append(rp.buttonTag('default', 'Camelot!', '70', '100', 'The television reception isnt good here anyway', ['FORM', FORM], FORM))
  • Now that we have some very pythonic widgets in our layout, we should find an end by closing the layout and initializing it, so that it can be loaded:
out.append(rp.closeLayout(LAYOUT))
out.append(rp.init(LAYOUT))
  • To make the HTML-String complete, we add the following:
out.append('</head>\n')
out.append('<body onLoad="init();">\n</body>\n</html>')
  • Last but not least, return the HTML-String to the browser:
return ''.join(out)

Make the buttons work

  • It's fine to have some buttons on a form, but only if they work… 8-O
  • To handle the asynchronous request, we have to define another method in our script, that in our case will answer with a rialto alert box
  • We assume two paremeters in the request: login and action. Login was the name of the textfield, action is produced automatically by the API and should be filled with the name of the pressed button. Our function then looks that way:
def do(self,login,action):
    res=rialtoPython.RialtoPython()
    out=[]
    error_msg=[]
    if action=='doLogon':
        if login=="":
            error_msg.append("Empty Spam")
        elif login!="Eggs":
            error_msg.append("Unknown Spam")
 
        if len(error_msg)>0:
            out.append(res.showAlert('myAlert', ','.join(error_msg)))
        else:
            out.append(res.showAlert('myAlert', 'This Spam was good!'))
    elif action == 'default':
        out.append(res.showAlert('myAlert', 'Are you sure, you want to go to camelot?'))
 
    else:
        out.append(res.showAlert('myAlert', 'I dont know anything about the action %s' % action))
 
    return ''.join(out)
  • As you see, the method returns an alert box depending on the button and the value of the login field.
  • And… Don't forget to expose the new method, so that CherryPy will find it:
do.exposed = True
  • Because now we have static content as well, we will have to add a configuration to our little CherryPy application by inserting the following in the main-sequence:
cherrypy.config.update(file = 'project.conf')
  • The configuration-file 'project.conf' will have to look like that:
[global]
server.socketPort = 8080
server.threadPool = 10
server.environment="production"
server.logFile="cherry.log"
staticFilter.root = "/somewhere/on/your/machine/RialtoDemo"
 
[/rialtoEngine]
staticFilter.on=True
staticFilter.dir="rialtoEngine"
 
[/config.js]
staticFilter.on=True
staticFilter.file="config.js"
 
[/rialto.js]
staticFilter.on=True
staticFilter.file="rialto.js"
 
[/images]
staticFilter.on=True
staticFilter.dir="images"
 
[/javascript]
staticFilter.on=True
staticFilter.dir="javascript"
 
[/style]
staticFilter.on=True
staticFilter.dir="style"

Just to make it complete: the whole code!

#!/usr/bin/python
# -*- coding: ISO-8859-1 -*-
 
import cherrypy
from rialtoPython import *
 
class Window:
 
    def index(self):
        DIV                 = "document.body"
        LAYOUT              = "ihmDemo"
        WINDOW              = "myWindow"
        WINDOW_TITLE        = "My First Pythonic Rialto Window"
        FRAME               = "boxDemo"
        FORM                = "myForm"
        URL                 = "do"
 
        rp = rialtoPython.RialtoPython()
 
        out = []
 
        out.append(rp.addImport(devMode=False))
        out.append('<script>')
        out.append('\trialto.I18N.setLanguage("en");\n')
        out.append(rp.openLayout(LAYOUT))
 
        out.append(rp.windowTag(WINDOW, WINDOW_TITLE, DIV))
 
        out.append(rp.frameTag(FRAME, '30', '30', '300', '120', 'My 1st python Box', 'true', 'false', 'relative', 'false', WINDOW))
        out.append(rp.formTag(FORM,URL,FRAME))
        out.append(rp.labelTag('label1', '25', '10', 'Spam', FORM))
        out.append(rp.textTag('login', '25', '90', '200', 'A', FORM, 'true', 'true', 'Eggs'))
 
        out.append(rp.buttonTag('doLogon', 'Ni!', '70', '10', 'And now to something completely different', ['FORM', FORM], FORM))
        out.append(rp.buttonTag('default', 'Camelot!', '70', '100', 'The television reception isnt good here anyway', ['FORM', FORM], FORM))
        out.append(rp.closeLayout(LAYOUT))
 
        out.append(rp.init(LAYOUT))
 
        out.append('</head>\n')
        out.append('<body onLoad="init();">\n</body>\n</html>')
 
        return ''.join(out)
 
    index.exposed = True
 
    def do(self,login,action):
        res=rialtoPython.RialtoPython()
        out=[]
        error_msg=[]
        if action=='doLogon':
            if login=="":
                error_msg.append("Empty Spam")
            elif login!="Eggs":
                error_msg.append("Unknown Spam")
 
            if len(error_msg)>0:
                out.append(res.showAlert('myAlert', ','.join(error_msg)))
            else:
                out.append(res.showAlert('myAlert', 'This Spam was good!'))
        elif action == 'default':
            out.append(res.showAlert('myAlert', 'Are you sure, you want to go to camelot?'))
 
        else:
            out.append(res.showAlert('myAlert', 'I dont know anything about the action %s' % action))
 
        return ''.join(out)
    do.exposed = True
 
cherrypy.root = Window()
 
if __name__ == '__main__':
 
    cherrypy.config.update(file = 'project.conf')
 
    cherrypy.server.start()
 

Let it roll!

  • To let the whole thing run in your browser, just download the javascript-api source distribution and unpack it in your directory. You then should have a folder “rialtoEngine” available.
  • Ready! 8-)
And just to see what it should look like:

python/tutorial.txt · Last modified: 2007/07/26 17:11 (external edit)
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0