CherryPy: Object-Oriented Web Development - Expanding Your Application (
Page 3 of 4 )
Of course, having just one method is useless and wasteful. Additional methods may certainly be created, and they may be access by appending their names to the address. For example, let's create a file that contains multiple methods and test it out:
import cherrypy
class MultiMethod:
def index ( self ):
return "<a href='A'>( Method A )</a>"
def A ( self ):
return "<a href='B'>( Method B )</a>"
def B ( self ):
return "<a href='C'>( Method C )</a>"
def C ( self ):
return "<a href=''>( Index )</a>"
index.exposed = A.exposed = B.exposed = C.exposed = True
cherrypy.root = MultiMethod()
cherrypy.config.update ( file = 'development.conf' )
cherrypy.server.start()
As you can see, each method contains a link to the next method. Of course, having a bunch of methods thrown around a file isn't going to help you when it comes to organization. That's why CherryPy also allows for multiple objects in your applications, as seen here:
import cherrypy
class DefaultClass:
def index ( self ):
return "<a href='A'>( Object A )</a>"
index.exposed = True
class A:
def index ( self ):
return "<a href='a'>( Method a )</a>"
def a ( self ):
return "<a href='/B'>( Object B )</a>"
index.exposed = a.exposed = True
class B:
def index ( self ):
return "<a href='b'>( Method b )</a>"
def b ( self ):
return "<a href='/C'>( Object C )</a>"
index.exposed = b.exposed = True
class C:
def index ( self ):
return "<a href='c'>( Method c )</a>"
def c ( self ):
return "<a href='/index'>( Index )</a>"
index.exposed = c.exposed = True
cherrypy.root = DefaultClass()
cherrypy.root.A = A()
cherrypy.root.B = B()
cherrypy.root.C = C()
cherrypy.config.update ( file = 'development.conf' )
cherrypy.server.start()
If you run the application, you will see that we have various objects and their methods mapped to different addresses:
http://localhost:8080
http://localhost:8080/A
http://localhost:8080/A/a
http://localhost:8080/B
http://localhost:8080/B/b
http://localhost:8080/C
http://localhost:8080/C/c
Notice that we assign an instance of each class to a variable under root. This is necessary for CherryPy to figure out which objects you want to let users access and where you want each object in the address map. You could also nest objects within the map:
import cherrypy
class DefaultClass:
def index ( self ):
return "<a href='A'>( Object A )</a>"
index.exposed = True
class A:
def index ( self ):
return "<a href='B'>( Object B )</a>"
index.exposed = True
class B:
def index ( self ):
return "<a href='C'>( Object C )</a>"
index.exposed = True
class C:
def index ( self ):
return "<a href='/'>( Index )</a>"
index.exposed = True
cherrypy.root = DefaultClass()
cherrypy.root.A = A()
cherrypy.root.A.B = B()
cherrypy.root.A.B.C = C()
cherrypy.config.update ( file = 'development.conf' )
cherrypy.server.start()
Now, C is inside B, which is inside A:
http://localhost:8080
http://localhost:8080/A
http://localhost:8080/A/B
http://localhost:8080/A/B/C