[Twisted-web] Parameters for data_/render_

Stumpe, Werner wstumpe at psipenta.com
Fri Mar 31 02:26:38 CST 2006


Hi,

 

I am working on a project (nevow + twisted) where lots of statistical
computing is done using "R", interfaced by rpy.py. It works very well.
To be able to parameterize the computing, I defined an additional
namespace and put the parameters with this namespace inside the nevow
templates. It looks like:

 

<html xmlns:nevow="http://nevow.com/ns/nevow/0.1" 

         xmlns:new_namespace="http://new_namespace.de/ns/0.1"

         xmlns="http://www.w3.org/1999/xhtml"

         xml:lang="de" lang="de"> 

<head>

  <meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type" />

</head>

<body>

<img

    new_namespace:title="Fehlerklasse 2 (letzte 50)" 

    new_namespace:table="defect"

    new_namespace:fields="measurement_id gravity" 

    new_namespace:reorder_x="1" 

    new_namespace:reverse_x="1" 

    new_namespace:sql="class = 2 ORDER BY _id DESC LIMIT 50"

    new_namespace:function="errorplot"

    new_namespace:smooth="2"

    nevow:data="Measurement" 

    nevow:render="Plot">

</body>

</html>

 

The parameters are extracted like this:

 

    def data_Measurement (self, context, data):

        """Extracts the parameters from the attribute-list.". 

        

        self.html_params = {}

        self.script_params = None            

        todelete = []

        for theKey in context.tag.attributes:

            keyFound = False 

            if type(theKey) is tuple:

                if len(theKey) == 2:

                    ns = theKey[0]

                    tag = theKey[1]

                    if ns == u'http:// new_namespace.de/ns/0.1' :

                       name = tag

                       value = context.tag.attributes.get(theKey)

                       self.script_params[name] = value

                       ##

                       ## Append the key to the list of keys to remove
later.

                       ## Unfortunately the parameters must be cleared,
so that only valid attributes

                       ## according the html-element are present in the
rendering step.

                       ## 

                       todelete.append(theKey)

        ##

        ## Delete the param-keys from the attribule-list.

        ## Otherwise the attributes would be present in the rendering
and this is for the birds. 

        ##

        for theKey in todelete:

            del context.tag.attributes[theKey]

 

In the function render_measurement() the function is called with the
parameters (in this case "errorplot") and the resulting image is
rendered. This also works very well. My question is: Is there a better
and simpler way to do that? And if not, why do I have to remove the
additional attributes, because later on nevow gets confused about this
attributes and produces a nice stack-trace?

 

The next question is: In the render_whatever() functions an image is
produced, and I want to write the image directly with perhaps
request.write(). How can this be done using nevow?

 

Thanks,

Werner

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20060331/5706f77c/attachment.htm


More information about the Twisted-web mailing list