[Twisted-web] HTML Rendering with Twisted.Web

Lucas Taylor ltaylor.volks at gmail.com
Fri Jun 28 16:33:10 MDT 2013


Hi Jo,

On Jun 26, 2013, at 10:52 AM, Jo as Queeniebee wrote:

> Hello,
> 
> I've asked a question before about using Jinja2 with Twisted.Web, but now I have a more general question about rendering HTML docs within a Twisted web application.
> 
> All the examples on TwistedMatrix.com and the txTemplate adapter show that you pass the web page by either returning it as a string from render_Get or as a param in .render(). Is this true for all cases?

A Resource should either directly return bytes from a render* method, OR, return twisted.web.server.NOT_DONE_YET to indicate an asynchronous response will be returned *at some point*.  

API:  http://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html#render

Also, these howtos may be useful to clarify the concept:
http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html
http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html


If you're just loading up a template from disk and rendering it with local data, you may find that synchronous load/render/return works fine. If you find that you are blocking the web server from servicing other requests because you are querying a database, or fetching something from a remote service or something, then you'll want to go the asynchronous route.

(Note that in your code below, there is really no reason to do it asynchronously)


> This seems a bit inefficient when you have a file larger than <html><body> Hello, World!</body></html>.

If you are planning on streaming large amounts of data, then Twisted's producer/consumer api may be helpful:
http://twistedmatrix.com/documents/current/core/howto/producers.html



> I've written an HTML file complete with .css and .js. How to I pass that document to my Twisted.Web resource? When I use the code below, the context replaces all markup from my HTML file. How do I get around this? How do you pass a larger HTML file to Twisted.Web?
> 
> # -*- coding: utf-8 -*
> 
> import os, sys
> 
> from twisted.application import internet
> from twisted.web.resource import Resource
> from twisted.web import server
> from twisted.internet import reactor
> 
> import txtemplate
> 
> TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),"templates")
> 
> class ThreadResource(Resource):
>     def __init__(self):
>         resource.Resource.__init__(self)
>         self.loader = txtemplate.Jinja2TemplateLoader(TEMPLATE_DIR)
>     
>     def getChild(self, name, request):
>          return self
> 	
>     def render_GET(self, request):
>     	template_name = "base.html"
>         template = self.loader.load(template_name)
>         context = {"greeting": "Enter"}
>  
>         def cb(content):
>             request.write(content)
>             request.setResponseCode(200)
>             request.finish()
>  
>     d = template.render(**context)
>         d.addCallback(cb)
>         return server.NOT_DONE_YET
> 
> site = server.Site(ThreadResource())
> reactor.listenTCP(8888, site)
> reactor.run()
> _______________________________________________



Your code looks fine (w/ a minor fixup in __init__).  I did a quick test using your code and the context rendered fine. e.g. in 'base.html' I just have:

Greeting:  {{ greeting }}

Maybe your template is malformed? 



Lucas



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-web/attachments/20130628/081a0efd/attachment.html>


More information about the Twisted-web mailing list