some basic questions for The Newbie's Guide to Cherrypy

282 views
Skip to first unread message

brahmaforces

unread,
Aug 13, 2007, 7:13:17 AM8/13/07
to cherrypy-users
This post asks some very basic questions about CP to get started with
the Newbie Documentation as discussed in the prior post, Is Cherrypy
getting overweight?

An example is used. The example here is to call a method from an html
page, where the method calls another html page.

What happens on running the code:
---------------------------------------------------
When calling a method from an html page I dont get an error and its
doesnt call the method. The 1st html page is rendered.,Also the server
starts up but gives the following comments:

"The application mounted at ' ' has an empty config. It looks like the
config you passed to cherrypy.config.update() contains application-
specific sections. You must explicitly pass application config via
cherrypy.tree.mount(...,config=app_config)"

Am using cherrytemplate because it is the most straightforward and no
templating language issues will get mixed into the analysis.Am not
using a string for html because that does not illustrate at a
sufficient level of detail for newbies.

Below are
* 2 html files (The first has a hyperlink(href) which when clicked
should call a function that should render the 2nd html page)
* the python file
* the config file (which by the way is in the same directory as the
python file)

1st html file, arjunTest.html:
----------------------------------------
<html><body>
<a href="<py-exec="root.addArtWork">">Output</a>
</body></html>


2nd html file, arjunTestOutput.html:
----------------------------------------------------
<html><body>
Hello Arjun!
</body></html>

The python file:
------------------------
import cherrypy
import cherrytemplate
from cherrytemplate import renderTemplate

class SamplesSite:

@cherrypy.expose
def index(self):
return renderTemplate(file='html/arjunTest.html')

@cherrypy.expose
def addArtWork(self):
print "I got here!"
return renderTemplate(file='html/arjunTestOutput.html')

#Assign site root to a variable root
root=SampleSite()

#The code to point to config file and start server
cherrypy.tree.mount(root)
if __name__ == '__main__':
import os.path

cherrypy.config.update(os.path.join(os.path.dirname(__file__),brahmaSite.conf'))
cherrypy.server.quickstart()
cherrypy.engine.start()

The configuration file:
------------------------------
[global]
server.socket_host = "localhost"
server.socket_port=8080
server.thread_post=10

[/]
tools.staticdir.root="/home/arjun/brahmaforces/source/pyfiles"

[/style.css]
tools.staticfile.on=True
tools.staticfile.filename="home/arjun/brahmaforces/source/pyfiles/html/
css/css.css"

[/html]
tools.staticdir.on=True
tools.staticdir.dir="html"

[/pics]
tools.staticdir.on=True
tools.staticdir.dir="html/pics"

Questions:
---------------

1) Is the config file ok? If yes then why the server message saying it
is empty?
2) If the config file lives in the same directory as th py file, is
all the path stuff to the config file necessary? If so, is it
presented in the cleanest possible way above?
3) What does the __name__... buy us, I saw this in the python docs,
what exactly is this doing for us here, in the cherrypy context and
the context of this module?
4) When I run the py file, the html page showing OUTPUT as a hyperlink
shows up. On clicking the link the method addArtWork is NOT called.
The server does not print "It got here". Why is the function not being
called?
5) Is this the right way to link to another page that is evaluated by
a python method or is there a simpler cleaner way to link to another
page that is a python method in the same class as the root class? The
idea being to simply be able to call methods and classes from html
pages and they doing some tasks and rendering other pages.
6) Is this mental model of thinking about cherrypy in terms of html
pages, classes and methods ok? How does it relate to the Cherry tree?
7) In the first html file why can't i simply use "addArtWork" instead
of "root.addArtWork" given that the page was called from root so the
method addArtWork, as a local method, should still be in scope. Is
there a better way to refer or call the method from addArtWork/
8) Whats a clean way to make this happen given that we use html pages
and a templating languaging rather than strings. It seems to work with
strings as in tut02. (tutorial 2 that lies in the tutorials folder
that comes with cherrypy 3)

Answers to these questions may clarify my mental model of cherrypy and
will be the start to the documentation for newbies...

Sylvain Hellegouarch

unread,
Aug 13, 2007, 8:30:49 AM8/13/07
to cherryp...@googlegroups.com
brahmaforces a écrit :

This is wrong. This should be:

<a href="/addArtWork">Output</a>


No need for the py-exec here.

The config file must be passed to tree.mount() as well to be applied to
the mounted application.

http://www.cherrypy.org/wiki/ConfigAPI

> The configuration file:
> ------------------------------
> [global]
> server.socket_host = "localhost"
> server.socket_port=8080
> server.thread_post=10
>
> [/]
> tools.staticdir.root="/home/arjun/brahmaforces/source/pyfiles"
>
> [/style.css]
> tools.staticfile.on=True
> tools.staticfile.filename="home/arjun/brahmaforces/source/pyfiles/html/
> css/css.css"
>
> [/html]
> tools.staticdir.on=True
> tools.staticdir.dir="html"
>
> [/pics]
> tools.staticdir.on=True
> tools.staticdir.dir="html/pics"
>
>
> Questions:
> ---------------
>
> 1) Is the config file ok? If yes then why the server message saying it
> is empty?
>

Because cherrypy.config.update only applies to the server not the
mounted application. Pass the config file to tree.mount()

> 2) If the config file lives in the same directory as th py file, is
> all the path stuff to the config file necessary? If so, is it
> presented in the cleanest possible way above?
>

The path must be absolute in CP simply because of a security problem we
had in the past with CP2.

Cleanest... well that's a matter of taste. I personally use a
dictionnary rather than a file for the config and therefore declare some
kind of base_path and use os.path.join(base_path, relative_dir) heavily.

> 3) What does the __name__... buy us, I saw this in the python docs,
> what exactly is this doing for us here, in the cherrypy context and
> the context of this module?
>

As much as I agree on helping people, there are something you should
lookup by yourself please.

This is a check that is done by Python when the module is loaded. If it
is loaded as a script to be run (say from the command line), the code
below the if statement is run.

> 4) When I run the py file, the html page showing OUTPUT as a hyperlink
> shows up. On clicking the link the method addArtWork is NOT called.
> The server does not print "It got here". Why is the function not being
> called?
>

Because your template is wrong. See above.

> 5) Is this the right way to link to another page that is evaluated by
> a python method or is there a simpler cleaner way to link to another
> page that is a python method in the same class as the root class? The
> idea being to simply be able to call methods and classes from html
> pages and they doing some tasks and rendering other pages
>

See above.

> 6) Is this mental model of thinking about cherrypy in terms of html
> pages, classes and methods ok? How does it relate to the Cherry tree?
>

I don't understand the question.

> 7) In the first html file why can't i simply use "addArtWork" instead
> of "root.addArtWork" given that the page was called from root so the
> method addArtWork, as a local method, should still be in scope. Is
> there a better way to refer or call the method from addArtWork/
>

See above again.


Arnar Birgisson

unread,
Aug 13, 2007, 8:59:51 AM8/13/07
to cherryp...@googlegroups.com
Hi there,

Replying to select questions as Sylvain addressed most of them.

On 8/13/07, brahmaforces <brahma...@gmail.com> wrote:
> 3) What does the __name__... buy us, I saw this in the python docs,
> what exactly is this doing for us here, in the cherrypy context and
> the context of this module?

When you "import" a module, it's __name__ parameter contains it's
name. If you run a .py file directly, it's __name__ parameter will
contain the string "__main__".

See here: http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm

This is basic python stuff that does not belong in the CherryPy
documentation imo.

> 6) Is this mental model of thinking about cherrypy in terms of html
> pages, classes and methods ok? How does it relate to the Cherry tree?

Not sure what your complete mental model is. The "tree" is there to
allow you to deploy multiple cherrpy "applications" or "sub-sites"
within one running server. It also allows you to "mount" third-party
WSGI compliant applications at specific urls in your url namespace.

To get regular CP 2 behaviour, just mount your root controller at '/'
and dispatching within that is done exactly the same as in CP 2, i.e.
it traverses a hierarchy of object instances down to a @exposed
method.

The tree can be explained in this way:

Say you write a blog in CherryPy. Now, I've written a forum software
in CP as well. If you wanted to integrate my forum software on your
blog website, you could use cherrypy.tree to "mount" my forum software
under the url http://yoursite.com/forums/ - and all urls beginning
with that will be dispatched seperately within my forums app, while
other urls go to your blog app (which most likely is mounted under
'/').

One nice thing is that even if both your blog and my forum uses the
config parameter "database.uri" - you can keep the two databases
seperated as they (can) have completely different config files each.

Furthermore, say you wanted to host a Trac instance on your site as
well. Trac is a WSGI compliant webapp, so you could use
cherrypy.tree.graft to place the Trac webapp under
http://yoursite.com/trac/.

In short, you use cherrypy.tree to combine several "applications"
together on one server, where each application is kept separate which
may be necessary if they come from different sources.

Within one application, the default dispatching strategy is the same as in CP2.

As a side note, with CP 3 the dispatching mechanism was made modular -
so it's easy to change it out. Your blog app might use the standard
MethodDispatcher a'la CP2, but my forum app might use the Routes based
RoutesDispatcher to map urls to methods.

Hope that helps improve your mental map.

Arnar

Robert Brewer

unread,
Aug 13, 2007, 11:13:47 AM8/13/07
to cherryp...@googlegroups.com
Sylvain Hellegouarch wrote:
> brahmaforces a écrit :

> > 1) Is the config file ok? If yes then why the server
> > message saying it is empty?
>
> Because cherrypy.config.update only applies to the server not
> the mounted application. Pass the config file to tree.mount()

If you're not used to the separation between "site" and "app", this
can be really confusing. I think we should start consistently using
the same formulae for sample code; either use cherrypy.quickstart,
or put calls in a particular order: set up site, set up apps, start.

# Set up site
cherrypy.config.update(siteconf)

# Set up applications
cherrypy.tree.mount(root, '/', appconf)

# Start the site
cherrypy.server.quickstart()
cherrypy.engine.start()

Robert Brewer
fuma...@aminus.org

winmail.dat

fumanchu

unread,
Aug 13, 2007, 11:28:18 AM8/13/07
to cherrypy-users
On Aug 13, 4:13 am, brahmaforces <brahmafor...@gmail.com> wrote:
> Am using cherrytemplate because it is the most straightforward and no
> templating language issues will get mixed into the analysis.Am not
> using a string for html because that does not illustrate at a
> sufficient level of detail for newbies.

It's also not supported by anyone, and newbies will be confused into
thinking that cherrytemplate is either bundled with or supported by
CherryPy or both.

If you want something super-simple, try http://projects.amor.org/misc/browser/cobbler.py
But personally, I'd rather see a modern, supported, recommended
templating tool like Mako get top billing.

> #The code to point to config file and start server
> cherrypy.tree.mount(root)
> if __name__ == '__main__':
> import os.path
>
> cherrypy.config.update(os.path.join(os.path.dirname(__file__),brahmaSite.conf'))
> cherrypy.server.quickstart()
> cherrypy.engine.start()

This should be:

import os
thisdir = os.path.dirname(__file__)
confpath = os.path.join(thisdir, 'mysite.conf')
cherrypy.quickstart(root, '/', confpath)

Once that works, you can later introduce the expansion of quickstart
into:

cherrypy.config.update(siteconf)
cherrypy.tree.mount(root, '/', appconf)
cherrypy.server.quickstart()
cherrypy.engine.start()

> [global]
> server.socket_host = "localhost"
> server.socket_port=8080
> server.thread_post=10

I assume you mean 'thread_pool'. But this defaults to 10, so it's a
bit redundant.

> 6) Is this mental model of thinking about cherrypy in terms of html
> pages, classes and methods ok? How does it relate to the Cherry tree?

It's fine. The tree is just where you mount the "root".


Robert Brewer
fuma...@aminus.org

brahmaforces

unread,
Aug 14, 2007, 6:05:22 AM8/14/07
to cherrypy-users
Great...a huge thank you to all the old dogs for making this
happen...I personally think this should really be of help to the
average programmer looking to deploy on the web...and if this works
out to be as simple and comprehensive as I am envisoning it to be, for
the new cherrypy user, I think it will win over the python programmers
looking to get started quickly and painlessly with a python web
framework.

I am working through all the replies here to create the starting
points for the lessons. I would like to make them super organized,
very simple to understand and quite comprehensive over time.

> It will be much more detailed though than the tutorials...

>>If so, it should probably be multiple pages. I recommend we
>>start a set of pages at http://www.cherrypy.org/wiki/lessons/*
>>or some similar subdirectory.

>> However in order to make this happen, users on this group
>>and the old dogs will have to clarify issues as they arise...
>> If I dont understand it I cant document it...What do you say?

>Sounds good to me.
>Robert Brewer

So i have started the document. Let me know where, how and when I
should put it up...Should I put it up now, as it moves along, or when
it reaches a certain point....In any case I will need to continue to
revise, add to it and fine tune it...It may be best to work on
iteratively and continue to add to it on an ongoing basis.


>If you want something super-simple, try http://projects.amor.org/misc/browser/cobbler.py
>But personally, I'd rather see a modern, supported, recommended
>templating tool like Mako get top billing.

Point noted, will install Mako and use if you think thats a good
templating system...


Reply all
Reply to author
Forward
0 new messages