File: gettingstarted.rst

package info (click to toggle)
circuits 3.1.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,756 kB
  • sloc: python: 15,945; makefile: 130
file content (67 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
.. _web_getting_started:

Getting Started
===============

Just like any application or system built with circuits, a circuits.web
application follows the standard Component based design and structure
whereby functionality is encapsulated in components. circuits.web
itself is designed and built in this fashion. For example a circuits.web
Server's structure looks like this:

.. image:: ../images/CircuitsWebServer.png

To illustrate the basic steps, we will demonstrate developing
your classical "Hello World!" applications in a web-based way
with circuits.web

To get started, we first import the necessary components:

.. code-block:: python
   
   from circutis.web import Server, Controller
   

Next we define our first Controller with a single Request Handler
defined as our index. We simply return "Hello World!" as the response
for our Request Handler.

.. code-block:: python
   
   class Root(Controller):
   
      def index(self):
         return "Hello World!"
   

This completes our simple web application which will respond with
"Hello World!" when anyone accesses it.

*Admittedly this is a stupidly simple web application! But circuits.web is
very powerful and plays nice with other tools.*

Now we need to run the application:

.. code-block:: python
   
   (Server(8000) + Root()).run()
   

That's it! Navigate to: http://127.0.0.1:8000/ and see the result.

Here's the complete code:

.. code-block:: python
   :linenos:

   from circuits.web import Server, Controller
   
   class Root(Controller):
   
      def index(self):
         return "Hello World!"
   
   (Server(8000) + Root()).run()
   

Have fun!