Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Application Object

CherryPy collects controllers and handlers into Application objects. You can construct your own cherrypy.Application(root, script_name) objects, or let CherryPy do it behind the scenes for you.

If you're simply using cherrypy.quickstart, you may not be aware that Application objects even exist. But the quickstart function calls tree.mount, which constructs an Application object behind the scenes. If you call tree.mount directly, it will return the Application object to you. Application objects are also available in the tree.apps dict (see below). These are all available as soon as you mount an application.

You can also access the current Application object at runtime via cherrypy.request.app.

Attributes

root

By default, CherryPy uses a nested set of objects to define the URL space of your application. The Application.root should be the top-most object in the set.

script_name

The URL "mount point" for the application. For example, if script_name is "/my/cool/app", then the URL "http://my.domain.tld/my/cool/app/page1" might be handled by a "page1" method on the root object. If script_name is explicitly set to None, then the script_name will be provided for each call from request.wsgi_environ['SCRIPT_NAME'].

config

A dict of {path: pathconf} pairs, where 'pathconf' is itself a dict of {key: value} pairs. Any keys (config file section names) which are not paths (that is, do not start with a slash "/") are considered off-limits to the CherryPy framework. Feel free to define your own keys here for app-specific config.

namespaces

A _cpconfig.NamespaceSet?.

log

A LogManager? instance. See _cplogging.

Tree Object

The Tree class is used to keep track of where multiple applications are mounted. To "mount" an application means to have its root respond to a URL other than "/". By using cherrypy.tree, you can easily mount applications and remember where you mounted them!

mount(root, script_name="", config=None)

Function to mount a set of handlers at the given script_name, using the given configuration dict (or filename). If config is not None, then each of its sections (which should be a relative URL, like "/skins/deepblue/main") will be prefixed with the script_name, so that config lookups are also "mounted" at the base URL.

apps

A dict of the form {script name: application}, where "script name" is a string declaring the URI mount point (no trailing slash), and "application" is an instance of cherrypy.Application (or an arbitrary WSGI callable if you happen to be using a WSGI server).

script_name(path=None)

A method which finds the appropriate script_name for a given path. If path is None or missing, cherrypy.request.script_name is used. If multiple applications "contain" the given path, the longer script_name is returned. That is, if App1 is mounted at "/" and App2 is mounted at "/path/to/app", then cherrypy.tree.script_name("/path/to/app/main") will return "/path/to/app".

Once you have obtained the script_name, you can obtain a reference to the application root object by looking up cherrypy.tree.apps[script_name].root.

Hosted by WebFaction

Log in as guest/cpguest to create tickets