[Welcome] [TitleIndex] [WordIndex

Odds and sods.

Quixote Quickstart

Drafting a simple tutorial with the aim of either giving people a push if they've just started using Quixote, or just curious about the thing and want to see what it's about. Perhaps it could use it's own page (feel free to move it there) - all feedback welcome and encouraged.

This assumes you've got Quixote already installed.

Hello World

Honouring the great "Hello World" tradition, we'll start off simply by making a Quixote program that puts the text "Hello World" into the web browser.

So fire up your favourite editor and paste the following into a new file:

   1 from quixote.directory import Directory
   2 from quixote.publish import Publisher
   3 
   4 class MyApp(Directory):
   5     _q_exports = ['']
   6 
   7     def _q_index(self):
   8         return "Hello, World!"
   9 
  10 def create_publisher():
  11     return Publisher(MyApp())
  12 

Save it as qhelloworld.py then crank up a command line and go to wherever you saved that script then type:

python -m quixote.server.simple_server --factory=qhelloworld.create_publisher --port=8080

It should go quiet at this point; we've started up a simple web server that's waiting on port 8080. So let's bring up a web browser and have a look. Type http://localhost:8080 into the location bar and press return.

Hello, World!

Surprise surprise, we get the greeting "Hello, World!". To stop the server, press CTRL+C at the command line.

Let's look at the interesting bits of our program:

   1 class MyApp(Directory):
   2 

Here, we're defining "MyApp", it's a subclass of Directory. Generally in Quixote all your apps will do this, it makes them publishable.

   1     _q_exports = ['']
   2 

When it comes to publishing, _q_exports provides the server with a list of pages available for outputting to the web. If the page is not on the list, it's not coming out.

So here we see, we're publishing '' or just the index page.

   1     def _q_index(self):
   2 

This is perhaps a sticking point. Usually in Quixote, your method name is the same as the path in the URL. But in this case as we're dishing up '' so we can't really call our method '' can we? So in Quixote this is worked around by calling it _q_index.

   1         return "Hello, World!"
   2 

Now we'll make our method return a string. That string is then used as the output to the browser (as you saw).

   1 def create_publisher():
   2     return Publisher(MyApp())
   3 

This function is used by Quixote to create a Publisher instance, that contains MyApp. We'll get to that later.

More URLS

So far it's not the best of web apps. Sure, you could add chunky fonts, gradients and pastel shades and flog it to Google for a small fortune. But what if we want to add more URLS?

No problem. Just add them to _q_exports and provide the corresponding methods to render them:

   1 class MyMoreAdvancedApp(Directory):
   2     _q_exports = ['', 'another', 'bye']
   3 
   4     def _q_index(self):
   5         """A Classic Hello, World page."""
   6         return "Hello, World!"
   7 
   8     def another(self):
   9         """Show another page"""
  10         return "This is another page. Double the fun!"
  11 
  12     def bye(self):
  13         """Show a goodbye page"""
  14         return "I tire of you now. Begone!"
  15 

Now if you go to /, /another and /bye, you'll see the corresponding page and a silly one line example. As a small aside, notice I've used doc strings to give a brief description of what each page does. Sure, the examples here are simple, but on larger apps it'll give you a good idea of what's going on when you or someone else comes to look at the code again.

Even more URLs

So far we've made a page for every URL we specify. But try looking at a URL that's not defined in the program, we get a 404 error (try it).

For some applications that have set URLs, that's fine. But suppose we wanted to write a wiki and wanted the page name as part of the URL?

We can use the special function _q_lookup to provide this functionality.

   1 class WikiApp(Directory):
   2     _q_exports = ['regular']
   3 
   4     def regular(self):
   5         return "This is just a regular page."
   6     
   7     def _q_lookup(self, pagename):
   8         return "You wanted the page: %s" % pagename
   9 

... ... falling asleep!

todo:


CategoryHomepage


2010-09-22 22:14