Twisted.Web and Jinja2
Hello, I'm writing a content server to run on my Raspberry Pi and I'll be using Jinja2 as the templating engine. I'm new to Twisted so I'm having a hard time imaging the examples for TwistedMatrix.com scale. Are there any examples of a fully featured Twisted application that uses Jinja2 or any other templating engine like Mako? Thanks, Jo
On Jun 18, 2013, at 8:47 PM, Jo as Queeniebee wrote:
Hello,
I'm writing a content server to run on my Raspberry Pi and I'll be using Jinja2 as the templating engine. I'm new to Twisted so I'm having a hard time imaging the examples for TwistedMatrix.com scale.
Are there any examples of a fully featured Twisted application that uses Jinja2 or any other templating engine like Mako?
Hi Jo, You can return a string (e.g. a rendered jinja2 template) from the .render method of an IResource http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#a... So when you see a string being returned from a Resource in the docs, you can consider substituting the particulars of the template engine you want to use. Here's a very simple example of one approach using jinja2: ## myapp/config.py # # Define a jinja2 Environment somewhere # I specify a PackageLoader to look up templates from the templates folder in the myapp.web.static package from jinja2 import Environment, PackageLoader jinja = Environment(loader=PackageLoader('myapp.web.static', 'templates')) ---- ## myapp/web/views.py # from twisted.web.resource import Resource from myapp.config import jinja class MyResource(Resource): isLeaf = True template_name = "example.html" def render_GET(self, request): ctx = { 'content': "Templated content", 'extra_info': [1, 2, 3, 4, 5, 6] } template = jinja.get_template(self.template_name) return template.render(ctx)
This is great, thanks. I want to deploy a content server on my Raspberry Pi, do I need to write a separate file for the server/reactor that imports views.py and config.py? Jo On Wed, Jun 19, 2013 at 2:35 PM, Lucas Taylor <ltaylor.volks@gmail.com>wrote:
On Jun 18, 2013, at 8:47 PM, Jo as Queeniebee wrote:
Hello,
I'm writing a content server to run on my Raspberry Pi and I'll be using Jinja2 as the templating engine. I'm new to Twisted so I'm having a hard time imaging the examples for TwistedMatrix.com scale.
Are there any examples of a fully featured Twisted application that uses Jinja2 or any other templating engine like Mako?
Hi Jo,
You can return a string (e.g. a rendered jinja2 template) from the .render method of an IResource
http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#a...
So when you see a string being returned from a Resource in the docs, you can consider substituting the particulars of the template engine you want to use.
Here's a very simple example of one approach using jinja2:
## myapp/config.py # # Define a jinja2 Environment somewhere # I specify a PackageLoader to look up templates from the templates folder in the myapp.web.static package from jinja2 import Environment, PackageLoader jinja = Environment(loader=PackageLoader('myapp.web.static', 'templates'))
---- ## myapp/web/views.py # from twisted.web.resource import Resource from myapp.config import jinja
class MyResource(Resource): isLeaf = True template_name = "example.html"
def render_GET(self, request): ctx = { 'content': "Templated content", 'extra_info': [1, 2, 3, 4, 5, 6] } template = jinja.get_template(self.template_name)
return template.render(ctx)
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
On Jun 19, 2013, at 2:21 PM, Jo as Queeniebee <joasqueeniebee@gmail.com> wrote:
This is great, thanks.
I want to deploy a content server on my Raspberry Pi, do I need to write a separate file for the server/reactor that imports views.py and config.py?
Given that on a raspberry pi you might be somewhat resource-constrained and concerned about overhead of code resources, have you considered using Twisted's built-in template engine? <http://twistedmatrix.com/documents/current/web/howto/twisted-templates.html> You can certainly use Jinja2 with Twisted, but given that Twisted might load this to render some of its own internal content (like directory listings) it might be worthwhile to give it a try. -glyph
Alright, then I'll use Twisted's built-in! But I still have my same question: Should I write a separate file containing all the resources and another file with the server? To deploy a content server on the RPi, do I need to wrap all the files within a .tac file? Joelle On Wed, Jun 19, 2013 at 8:24 PM, Glyph <glyph@twistedmatrix.com> wrote:
On Jun 19, 2013, at 2:21 PM, Jo as Queeniebee <joasqueeniebee@gmail.com> wrote:
This is great, thanks.
I want to deploy a content server on my Raspberry Pi, do I need to write a separate file for the server/reactor that imports views.py and config.py?
Given that on a raspberry pi you might be somewhat resource-constrained and concerned about overhead of code resources, have you considered using Twisted's built-in template engine?
< http://twistedmatrix.com/documents/current/web/howto/twisted-templates.html
You can certainly use Jinja2 with Twisted, but given that Twisted might load this to render some of its own internal content (like directory listings) it might be worthwhile to give it a try.
-glyph
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
On Jun 19, 2013, at 7:28 PM, Jo as Queeniebee wrote:
Alright, then I'll use Twisted's built-in! But I still have my same question: Should I write a separate file containing all the resources and another file with the server? To deploy a content server on the RPi, do I need to wrap all the files within a .tac file?
Joelle
This is highly dependent upon your application. Maybe you can contain everything you need to do in a single .py file and run it. Or maybe it would be beneficial to use separate modules or packages. These aren't really twisted-specific concerns. It's a matter of laying out your application so you can import the code you need, where you need it. If you have all of your Resources in a separate module or package, then you need to figure out how to import those and hook them up to the Site object. Extending the simple example from earlier, you might have a "run.py" in your app: ## myapp/run.py # from twisted.web import server from twisted.internet import reactor from myapp.views import MyResource site = server.Site(MyResource()) reactor.listenTCP(8080, site) reactor.run() This is highly simplified, but hopefully you can see it's just basic python. twisted.web.server.Site needs a Resource, so you have to figure out how to supply it. Could be in the same file, or could be importable from some other package or module. In this case, we've decided that we'll have a views module that contains MyResource. A .tac file is one way to handle application startup. You could also create a twistd plugin: https://twistedmatrix.com/documents/current/core/howto/tap.html. Or you could have a plain old python file with reactor.run() in it. This is prevalent in many of the howto examples and what the example above shows. I wouldn't really worry about .tac or plugins for the time being. At any rate, we're venturing into different territory now. You'll need to share some code (http://sscce.org/) or give a more complete description of what you're trying to do in order to get practical advice. It would be better to start a new thread with a clear intent and a different subject line, unless you still have questions about using jinja2. Lucas
Thanks, this has helped. When I share my code, is it better include a pastebin link or within the message? On Thu, Jun 20, 2013 at 1:47 AM, Lucas Taylor <ltaylor.volks@gmail.com>wrote:
On Jun 19, 2013, at 7:28 PM, Jo as Queeniebee wrote:
Alright, then I'll use Twisted's built-in! But I still have my same question: Should I write a separate file containing all the resources and another file with the server? To deploy a content server on the RPi, do I need to wrap all the files within a .tac file?
Joelle
This is highly dependent upon your application. Maybe you can contain everything you need to do in a single .py file and run it. Or maybe it would be beneficial to use separate modules or packages. These aren't really twisted-specific concerns. It's a matter of laying out your application so you can import the code you need, where you need it. If you have all of your Resources in a separate module or package, then you need to figure out how to import those and hook them up to the Site object.
Extending the simple example from earlier, you might have a "run.py" in your app:
## myapp/run.py # from twisted.web import server from twisted.internet import reactor
from myapp.views import MyResource
site = server.Site(MyResource()) reactor.listenTCP(8080, site) reactor.run()
This is highly simplified, but hopefully you can see it's just basic python. twisted.web.server.Site needs a Resource, so you have to figure out how to supply it. Could be in the same file, or could be importable from some other package or module. In this case, we've decided that we'll have a views module that contains MyResource.
A .tac file is one way to handle application startup. You could also create a twistd plugin: https://twistedmatrix.com/documents/current/core/howto/tap.html. Or you could have a plain old python file with reactor.run() in it. This is prevalent in many of the howto examples and what the example above shows. I wouldn't really worry about .tac or plugins for the time being.
At any rate, we're venturing into different territory now. You'll need to share some code (http://sscce.org/) or give a more complete description of what you're trying to do in order to get practical advice. It would be better to start a new thread with a clear intent and a different subject line, unless you still have questions about using jinja2.
Lucas
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
On Jun 20, 2013, at 12:29 PM, Jo as Queeniebee wrote:
When I share my code, is it better include a pastebin link or within the message?
Pastebins tend to disappear, so including a http://sscce.org/ in the message itself is better. You can always _additionally_ include a pastebin, but it's much better if the message contains all of the context needed to understand it. If I have to seek out a pastebin to review your code and it's down or deleted, I'll probably just ignore the thread. Also consider future readers of the list...it's very frustrating to find that a critical part of a conversation has gone missing. Also, in keeping with the idea that the message should be easy to understand and follow, the preference and convention for Twisted mailing lists is to bottom-post (http://en.wikipedia.org/wiki/Posting_style#Bottom-posting). Notice I've snipped out all of the extraneous stuff unrelated to this reply and just focused on your question. Lucas
I'm not sure how resource-constrained you'll be; If you run into problems, it might be interesting to deliver all your dynamic content as JSON, and offload the presentation to JS on the client side. On 21 June 2013 07:39, Lucas Taylor <ltaylor.volks@gmail.com> wrote:
On Jun 20, 2013, at 12:29 PM, Jo as Queeniebee wrote:
When I share my code, is it better include a pastebin link or within the message?
Pastebins tend to disappear, so including a http://sscce.org/ in the message itself is better. You can always _additionally_ include a pastebin, but it's much better if the message contains all of the context needed to understand it. If I have to seek out a pastebin to review your code and it's down or deleted, I'll probably just ignore the thread. Also consider future readers of the list...it's very frustrating to find that a critical part of a conversation has gone missing.
Also, in keeping with the idea that the message should be easy to understand and follow, the preference and convention for Twisted mailing lists is to bottom-post (http://en.wikipedia.org/wiki/Posting_style#Bottom-posting). Notice I've snipped out all of the extraneous stuff unrelated to this reply and just focused on your question.
Lucas
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
participants (4)
-
Donal McMullan
-
Glyph
-
Jo as Queeniebee
-
Lucas Taylor