Skip to content

Instantly share code, notes, and snippets.

@FND
Created June 28, 2009 08:50
Show Gist options
  • Select an option

  • Save FND/137223 to your computer and use it in GitHub Desktop.

Select an option

Save FND/137223 to your computer and use it in GitHub Desktop.
Nevow & Stan tutorial (EuroPython 2009)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Goodbye</title>
</head>
<body>
<h1>Goodbye</h1>
<p>See you later.</p>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h1>Hello</h1>
<p>Welcome!</p>
</body>
</html>
#!/usr/bin/env sh
twistd -noy server.py
from time import time
from twisted.application import internet, service
from nevow import appserver, loaders, tags, static, rend
class InfoTree(rend.Page):
addSlash = True
children = {
"help": static.File("./htdocs/help.html")
}
def locateChild(self, ctx, segments):
r = self.children.get(segments[0], None)
if r is not None:
return r, segments[1:]
class TimeDisplay(rend.Page):
docFactory = loaders.xmlfile("./templates/time.html")
def render_now(self, ctx, data):
return ctx.tag[unicode(time())]
class TopLevel(rend.Page):
addSlash = True
docFactory = loaders.stan(tags.html["Ths is the top-level resource."])
children = {
"info": InfoTree(),
"time": TimeDisplay(),
"hello": static.File("./htdocs/hello.html"),
"goodbye": static.File("./htdocs/goodbye.html")
}
def locateChild(self, ctx, segments):
if segments[0] == "":
return self, []
r = self.children.get(segments[0], None)
if r is not None:
return r, segments[1:]
if __name__ == "__builtin__":
# create application and give it a name
application = service.Application('helloworld')
# create a server and connect it to an object that will resolve URLs and
# serve up web pages (or other resources)
site = appserver.NevowSite(TopLevel())
# create a webserver instance, binding it to port and the site object
webServer = internet.TCPServer(8090, site)
# bind the application to the webserver
webServer.setServiceParent(application)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:n="http://nevow.com/ns/nevow/0.1">
<head>
<meta content="text/html; charset=utf-8" />
<title>The current time</title>
</head>
<body>
<h1>Time</h1>
<p>
The time is now <b n:render="now"></b>.
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment