most_basic.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » most_basic » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » examples » most_basic » most_basic.py
from zope.interface import implements

from nevow import inevow

##
## How does a request come to the Page?
##
## or How to use Nevow without all the fancy automations
##

# This is a simple Root page object, the inevow.IResource interface
# tells us that it must implement 2 methods:
# locateChild and renderHTTP.
# locateChild is used to find children of the current page, it must return a 
# tuple of (page, remaining_segments)
# if there is no page, and you want to display a 404 page, you will need to return
# a None, () tuple.
class Root(object):
    implements(inevow.IResource)

    def locateChild(self, ctx, segments):
        # This locateChild is 'stupid' since it can only work if the tree of
        # pages is static. Anyway it will work for our simple example
        if segments[0] == '':
            # If the server is looking for the root page segments will be ('',)
            # then renderHTTP will be called on self
            return self, ()
        elif segments[0] == 'foo':
            # Now we received a request whose segments had in the first position
            # the string foo
            # like http://example.org/foo/baz/ -> ('foo', 'baz')
            # after the page has been located we return it with the remaining segments
            # ('baz')
            return self.foo, segments[1:]
        else:
            return None, ()
        
    def renderHTTP(self, ctx):
        # When the server needs to return a response to the request it will call
        # the renderHTTP method that will return a string of what needs to be sent.
        return """<html><body>Hello world!<br />
        <a href="./foo" id="foo">foo</a></body></html>
"""

class Foo(object):
    implements(inevow.IResource)
    
    def locateChild(self, ctx, segments):
        # segments is the remaining segments returned by the root locateChild
        # see segments[1:]
        if segments[0] == 'baz':
            return self.baz, segments[1:]
        else:
            return None, ()
    
    def renderHTTP(self, ctx):
        return """<html><body><h1 id="heading">You are in Foo</h1>
        <a href="./foo/baz" id="baz">baz</a></body></html>
"""

class Baz(object):
    implements(inevow.IResource)
    def locateChild(self, ctx, segments):
        return None, ()
    def renderHTTP(self, ctx):
        return '<html><body><h1 id="heading">You are in Baz</h1></body></html>'

# We are adding children to the pages.
# This could also happen inside the class.
root = Root()
root.foo = Foo()
root.foo.baz = Baz()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.