3. Creating a first website: Hello, world !

At the same level as the demo/ directory, create a directory called hello/.

Go to the hello/ directory and create a file called Hello.cpy that contain the following lines:

CherryClass Root:
mask:
    def index(self):
        <html><body>
            Hello, world !
        </body></html>
3.1

To compile the file, type:

python ../cherrypy.py Hello.cpy

This will create a file called HelloServer.py, which contains everything to run the website (including an HTTP server). To start it, just type:

python HelloServer.py

To see the page, open a browser and type in the URL: http://localhost:8000/

What we've learned:

Now let's add some dynamic functionality to it...



Footnotes

... 3.1
CherryPy's policy is that one tab should be used to indent a block (it's easier to hit once the TAB key rather than 4 times the SPACE key). However, we know that some people have some reasons not to use tabs (for instance, sending a program with tabs by email or posting it to a newsgroup doesn't work very well). Therefore, it is also possible to use whitespaces to indent blocks in CherryPy's source files. By default, CherryPy will expect 4 whitespaces for one indentation. If you use something else (for instance, 3 whitespaces), then you have to use the -W 3 option to tell CherryPy about it. The way it works is very simple: when it reads a file, CherryPy's preprocessor starts by replacing all occurences of 4 whitespaces (or 3, depending on the -W option) with one tab. Please note that, unlike Python, one tab can never correspond to 2 indentation levels. It always corresponds to one indentation level.
See About this document... for information on suggesting changes.