paulb@348 | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@348 | 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paulb@348 | 3 | <head> |
paulb@348 | 4 | <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> |
paulb@348 | 5 | <title>Applications and Resources</title> |
paulb@348 | 6 | <meta name="generator" |
paulb@348 | 7 | content="amaya 8.1a, see http://www.w3.org/Amaya/" /> |
paulb@348 | 8 | <link href="styles.css" rel="stylesheet" type="text/css" /> |
paulb@348 | 9 | </head> |
paulb@348 | 10 | <body> |
paulb@348 | 11 | <h1>Applications and Resources</h1> |
paulb@348 | 12 | At its simplest a WebStack application is just a single Python |
paulb@349 | 13 | class that we call a "resource". This class can be defined inside a |
paulb@349 | 14 | normal Python module or package, so let us start by doing the following:<br /> |
paulb@349 | 15 | <ol> |
paulb@349 | 16 | <li>Create a new directory for |
paulb@349 | 17 | our application; choose any name since we just want an empty space in |
paulb@349 | 18 | which to put new files.</li> |
paulb@349 | 19 | <li>Create a file called <code>MyApplication.py</code> |
paulb@349 | 20 | - this is our module.</li> |
paulb@349 | 21 | </ol> |
paulb@349 | 22 | We are going to call our resource <code>MyResource</code> |
paulb@349 | 23 | and in principle it will have a structure that looks like this: |
paulb@349 | 24 | <pre>class MyResource:<br /> def respond(self, trans):<br /> [Examine the transaction, decide what the user wants to do.]<br /> [Perform some kind of action with the information supplied.]<br /> [Produce some kind of response which tells the user what happened.]</pre> |
paulb@349 | 25 | <p>It is in this kind of resource |
paulb@349 | 26 | class that we write the |
paulb@348 | 27 | actual application code or at least the beginnings of it. When a user |
paulb@348 | 28 | of the application sends us a request, |
paulb@349 | 29 | the <code>respond</code> method |
paulb@349 | 30 | will be called and the code |
paulb@348 | 31 | within it executed. The parts of the pseudo-code in |
paulb@348 | 32 | the above text which aren't valid Python |
paulb@349 | 33 | (ie. the bits in square brackets) will, when we have written them, use |
paulb@349 | 34 | the <code>trans</code> |
paulb@348 | 35 | object to find out what any given user of the application has sent us, |
paulb@348 | 36 | and to send information back |
paulb@348 | 37 | to the |
paulb@348 | 38 | user in response.</p> |
paulb@348 | 39 | <h2>Starting Simple</h2> |
paulb@349 | 40 | <p>The simplest way to turn this |
paulb@349 | 41 | into a working application is to |
paulb@348 | 42 | ignore the first two activities mentioned in the pseudo-code and just |
paulb@348 | 43 | to produce some kind of |
paulb@349 | 44 | response. Here is how we can make our application do something:</p> |
paulb@349 | 45 | <ol> |
paulb@349 | 46 | <li>Edit the module |
paulb@349 | 47 | file <code>MyApplication.py</code>.</li> |
paulb@349 | 48 | <li>Write into it the following |
paulb@349 | 49 | code which defines <code>MyResource</code>:</li> |
paulb@349 | 50 | </ol> |
paulb@348 | 51 | <pre>class MyResource:<br /> def respond(self, trans):<br /> out = trans.get_response_stream()<br /> print >>out, "Hello world."</pre> |
paulb@349 | 52 | <h2>Testing the Resource</h2> |
paulb@349 | 53 | <p>To test this resource we need to deploy it, and to do that we need |
paulb@349 | 54 | an |
paulb@349 | 55 | adapter. Here is a quick way of writing an adapter and testing this |
paulb@349 | 56 | code:</p> |
paulb@349 | 57 | <ol> |
paulb@349 | 58 | <li> Create a file called <code>MyAdapter.py</code> - you |
paulb@349 | 59 | can choose another name if you want - this will be where the adapter |
paulb@349 | 60 | code lives.</li> |
paulb@349 | 61 | <li>Copy the example adapter in <a href="deploying.html">"Deploying |
paulb@349 | 62 | a WebStack Application"</a> and write it into <code>MyAdapter.py</code>.</li> |
paulb@349 | 63 | <li>Now, with two files in your directory, <code>MyApplication.py</code> |
paulb@349 | 64 | and <code>MyAdapter.py</code>, you may run <code>MyAdapter.py</code> |
paulb@349 | 65 | as follows:</li> |
paulb@349 | 66 | </ol> |
paulb@349 | 67 | <pre>python MyAdapter.py</pre> |
paulb@349 | 68 | <p>This should start the adapter program and print the following |
paulb@349 | 69 | message:</p> |
paulb@349 | 70 | <pre>Serving...</pre> |
paulb@349 | 71 | <p>You should now be able to visit <code>http://localhost:8080</code> |
paulb@349 | 72 | in your |
paulb@349 | 73 | browser and see the message printed by your application:</p> |
paulb@349 | 74 | <pre>Hello world.</pre> |
paulb@349 | 75 | <h2>About Resources</h2> |
paulb@348 | 76 | <ul> |
paulb@349 | 77 | <li><a href="resource-creation.html">How Resources are Created</a></li> |
paulb@349 | 78 | <li><a href="design.html">Application Design Considerations</a></li> |
paulb@348 | 79 | </ul> |
paulb@348 | 80 | </body> |
paulb@348 | 81 | </html> |