Lesson 3: Web Development

Scenario

Quickly develop a web application or a web service.

Get started

Yotsuba 3 comes with a built-in web framework called Tori Web Framework.

As Yotsuba 3 is a pure-Python library, it does not supply with executable commands like a framework such as Pylons or Django. Nevertheless, it is not hard to get started.

First of all, you need to a script that calls yotsuba.lib.tori.setup with auto_config enabled. For example, you have a script called service.py at /home/dev/tori_app containing:

1
2
3
# service.py
from yotsuba.lib import tori
tori.setup('config.xml', auto_config=True)

Note

In order to save some unnecessary computation, the option auto_config is false by default.

Then from execute python service.py or python /home/dev/tori_app/service.py.

At this point, do not freak out as the service does not start. The script just created the configuration file config.xml which contains something similar to this

 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
<?xml version="1.0" encoding="UTF-8"?>
<webconfig>
    <mode>local</mode>
    <rendering>mako</rendering>
    <base_path>
        <static>static</static>
        <template>template</template>
        <session>memory</session>
    </base_path>
    <!--
    <port>8080</port>
    -->
    <base_uri>/</base_uri>
    <static_routing>
    <!--
        <file link="favicon.ico" ref="favicon.ico" />
        <dir link="image" ref="image" />
        <dir link="css" ref="css" />
        <dir link="js" ref="js" />
    -->
    </static_routing>
    <settings>
        <option name="debug">false</option>
        <option name="text_minification">false</option>
        <option name="no_cache">true</option>
    </settings>
</webconfig>

As it is, what does this file tell you? #. mode tells you what mode it is. There are three modes supported.

  • local (standalone, WSGI server)
  • app (WSGI application)
  • gae (WSGI application for Google App Engine)
  1. rendering tells you what rendering engine it uses. There are two engines automatically supported.
  • mako (Mako template)
  • tenjin (Tenjin template known as PyTenjin)
  1. base_path is telling you the base paths of static (static file), template (templates) and session (file-based session storage). These will be created on demand if not existed. Please note that these are based on the current working directory.
  2. port (optional) is telling you what port it is listening to. This only works in mode local.
  3. base_uri is telling you the base URI of the application.
  4. static_routing tells you where the static content is and how to get it. Please note that anything in static will be automatically mapped with the same path. See the next example on advanced routing.
  5. settings tells you the settings the app is using. There are three main settings.
  • debug is to enable debugging messages.
  • text_minification is to enable text minification (e.g. XML, HTML, CSS, JavaScript etc.)
  • no_cache is to disable in-memory caching mechanism. Primarily used to disable yotsuba.lib.tori.BaseInterface.cache.

Play around with configuration

Suppose you want to go fancy with the configuration by:

  1. run the app on port 80.
  2. change the location of the session storage to /tmp/tori_app/
  3. use other ICO file as favicon.ico
  4. rename the directory name in the static folder from very_ugly_name to something_better without physically renaming it.

Then, you will have something like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<webconfig>
    <mode>local</mode>
    <rendering>mako</rendering>
    <base_path>
        <static>static</static>
        <template>template</template>
        <session>/tmp/tori_app/</session>
    </base_path>
    <port>80</port>
    <base_uri>/</base_uri>
    <static_routing>
        <file link="favicon.ico" ref="favicon-beta.ico" />
        <dir link="something_better" ref="very_ugly_name" />
    </static_routing>
    <settings>
        <option name="debug">false</option>
        <option name="text_minification">false</option>
        <option name="no_cache">true</option>
    </settings>
</webconfig>

At this point, the application is still not runable. You need the next step to run.

Wait! “Hello, world!” again!?

Well, yes. Now, assume that you want to “Hello, world!” again with Tori.

Create an interface

Well, Tori interface is the same sense as controller in many web frameworks, view in Django or Servlet in Java.

In Yotsuba’s Tori, you can use a class inherited from

For starter, we will put everything in service.py.

1
2
3
4
5
6
7
8
# service.py
from yotsuba.lib import tori
tori.setup('config.xml') # disable auto_config

class HelloWorld(tori.BaseInterface):
    def index(self):
        return "Hello, world!"
    index.exposed = True

Please note that this is the same way you do in CherryPy 3.1+. Please visit http://cherrypy.org/wiki/CherryPyTutorial for more information.

Run the application

Finally, add application = tori.ServerInterface.auto(HelloWorld()) to start the application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# service.py
from yotsuba.lib import tori
tori.setup('config.xml') # disable auto_config

class HelloWorld(tori.BaseInterface):
    def index(self):
        return "Hello, world!"
    index.exposed = True

def main():
    application = tori.ServerInterface.auto(HelloWorld())

if __name__ == "__main__":
    main()

Run python service.py (or with the absolute path) again. Now, you should be able to access to your app at http://127.0.0.1/.

Note

The application is listening at 0.0.0.0:80.

What is so fascinating about Tori comparing to plain CherryPy?

CherryPy is a very good framework but it is like writing “Hello, world!” in Java comparing to Python. Yotsuba’s Tori is developed to reduce the complexity of getting started with CherryPy and make the development with the framework more enjoyable.

As Tori is developed as the wrapper of CherryPy, hence Tori is fully compatible with all tools available in CherryPy.

Tori is made for deployment into any platform with a single script.

Conclusion

To be frank with you, beside doing something very fancy, I (Juti, the author) can develop pretty much every thing with Yotsuba 3. These tutorials are only giving you some highlighted features of Yotsuba 3. You can explore more about other built-in on this site.

See also

Module yotsuba.lib.tori
Web framework