Quickly develop a web application or a web service.
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)
- mako (Mako template)
- tenjin (Tenjin template known as PyTenjin)
- 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.
Suppose you want to go fancy with the configuration by:
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.
Well, yes. Now, assume that you want to “Hello, world!” again with Tori.
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.
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.
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.
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