[Twisted-web] really beginner example

Ellers andrew at ellerton.net
Sun Apr 4 15:06:16 MDT 2004

Hello all,

I've been getting into Twisted apps recently and saw in the docs that use of Woven is not recommended and that Nevow should be used instead. Thats cool.

I've been trying to get into it, and have read lots of docs and examples, but its seriously hard going. I'm not trying to dump on anyone, but the examples don't start out with really simple building blocks then build on that into something big and usable as-is. Something I can say "I need to do a multistage form - ah theres an example", and "I need to store user sessions in a persistent way, so they're there after a database restart - ah an example..." etc.

That is, start with really really basic apps, adding extra details and features that someone will commonly want.

For example, it might start with a really simple html template example, using minimal Nevow stuff - just a renderer or at most a page, and everything else from Twisted. Maybe this isn't the "done" thing, but I think it would be really helpful to someone coming from now understanding Twisted classes to see how to use a minimal set of Nevow classes to do something cool with Nevow. I would've thought Nevow would work well in this way, but tell me if I'm wrong and I should really be using the whole Nevow framework, not just the xhtml templating part.

Right now I want to use my own Site object, not a subclass of the Nevow site object, and use the Nevow classes to generate html based on templates from files. I'm sure this is possible, but I just can't figure it out.

So, here is my question...

I have this in a file "hello_template.html":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
		<title nevow:data="title">This is title</title>
		<p>This page is called <span nevow:date="title" /></p>

and I have the following python code.
The function demo1() does what I expect,
but what I really want is for demo2() to print to stdout the xhtml
after it has been substituted with the title.

 from nevow import rend

class HelloPage(rend.Page):

	docFactory = rend.htmlfile('hello_template.html')
	def data_title(self, context, data):
		return "Hello Nevow World"
	def data_empty(self, context, data):
		return []

	def someMethodToWriteToAFileLikeObject( self, writer ):
		writer.write( "not yet implemented\n" )

def demo1( ):
	"""This will print the html just as it is in the template file - cool"""
	import sys
	d = rend.htmlfile('hello_template.html', '../shared/Assets/template')
	print "\nDocument:\n"
	d.getDoc( ).writexml(sys.stdout)
	print "\n"

def demo2( ):
	"""This does not work yet, but what I want it to do, is put the
	substituted title into the template then return the composite result.
	import sys
	p = HelloPage( )
	p.someMethodToWriteToAFileLikeObject( sys.stdout )
if __name__ == "__main__":
	demo1( )
	demo2( )

I know that in normal usage there isn't much point doing sys.stdout.write
with the xhtml content, but there has to be some room between that extreme
and using a framework where I can't even tell how the content is getting
sent back to the http stream!

Thanks to all for any suggestions, and for your patience with this beginner...


More information about the Twisted-web mailing list