Nagare - an example of using the framework
In my last post, I presented Nagare - a revolutionary (albeit unrivaled on Smalltalk and CL ) Python web framework. That post was somewhat messy and reflected, rather, the degree of my enthusiasm than the real features of the technology. Today I will try to give a slightly more practical example.
Start over. First of all, we need Stackless Python . To install on FreeBSD (hereinafter, I will use this system as a base, there shouldn’t be any differences for Linux, there are features for MacOS and Windows, but they are not critical) you need to download the distribution package: unzip it: configure it (we will install it in / usr / local / stackless)
compile and install: Actually, it would be most correct to install stackless-python through the ports system, but for some reason it is missing from it. Now we need to download and unzip virtualenv : then create a virtual “sandbox” in which Nagare will work (/ usr / local / nagare): Now, we need to install, in fact, Nagare itself: Everything, Nagare is installed and ready to work. Create an application: The test1 directory will be created containing the following files: ./test1/setup.py ./test1/test1/__init__.py ./test1/test1/app.py ./test1/test1/models.py ./test1/ static / __ init__.py ./test1/data/__init__.py ./test1/conf/__init__.py
./test1/conf/test1.cfg
The setup.py file contains all the service information such as a license, author name, email address, etc., you can leave it blank for a test application, it will work like that.
In the conf / test1.cfg file, enable debugging mode and connect the database (by default - sqlite). After corrections, it will look like this: Now we execute the command: Everything, the application is registered, you can test it: By default, the application will be available at \ http: //127.0.0.1: 8080 / test1. We proceed to the development. First we set up the database tables. To do this, edit the test1 / models.py file, adding table definitions (for those interested in details, I will refer to the Elixir documentation ). As a result, the file will look like this:
Let's create the database itself: Now we program the simplest guest book. To do this, edit test1 / app.py as follows:
Start over. First of all, we need Stackless Python . To install on FreeBSD (hereinafter, I will use this system as a base, there shouldn’t be any differences for Linux, there are features for MacOS and Windows, but they are not critical) you need to download the distribution package: unzip it: configure it (we will install it in / usr / local / stackless)
$ fetch \http://www.stackless.com/binaries/stackless-264-export.tar.bz2
$ tar -xvjf stackless-264-export.tar.bz2
$ cd stackless-2.6.4
$ ./configure --prefix=/usr/local/stackless
compile and install: Actually, it would be most correct to install stackless-python through the ports system, but for some reason it is missing from it. Now we need to download and unzip virtualenv : then create a virtual “sandbox” in which Nagare will work (/ usr / local / nagare): Now, we need to install, in fact, Nagare itself: Everything, Nagare is installed and ready to work. Create an application: The test1 directory will be created containing the following files: ./test1/setup.py ./test1/test1/__init__.py ./test1/test1/app.py ./test1/test1/models.py ./test1/ static / __ init__.py ./test1/data/__init__.py ./test1/conf/__init__.py
$ make
$ sudo make install
$ fetch \http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.5.tar.gz
$ tar -xvzf virtualenv-1.4.5.tar.gz
$ cd virtualenv-1.4.5
$ sudo /usr/local/stackless/bin/python ./virtualenv.py /usr/local/nagare
sudo /usr/local/nagare/bin/easy_install 'nagare[full]'
$ /usr/local/nagare/bin/nagare-admin create-app test1
Application 'test1' created.
Note:
1. Edit the file 'test1/setup.py' to set the informations about your new application.
2. Register your application with:
- cd "test1"
- "/usr/local/nagare/bin/python" setup.py develop
./test1/conf/test1.cfg
The setup.py file contains all the service information such as a license, author name, email address, etc., you can leave it blank for a test application, it will work like that.
In the conf / test1.cfg file, enable debugging mode and connect the database (by default - sqlite). After corrections, it will look like this: Now we execute the command: Everything, the application is registered, you can test it: By default, the application will be available at \ http: //127.0.0.1: 8080 / test1. We proceed to the development. First we set up the database tables. To do this, edit the test1 / models.py file, adding table definitions (for those interested in details, I will refer to the Elixir documentation ). As a result, the file will look like this:
[application]
path = app test1
name = test1
debug = on
[database]
activated = on
uri = sqlite:///$here/../data/test1.db
metadata = test1.models:__metadata__
debug = on
$ sudo /usr/local/nagare/bin/python ./setup.py develop
$ /usr/local/nagare/bin/nagare-admin serve test1
Application 'app test1' registered as '/test1'
03/17/10 18:00:35 - serving on \http://127.0.0.1:8080
from elixir import *
from sqlalchemy import MetaData
__metadata__ = MetaData()
class GuestBookRecord(Entity):
text=Field(Unicode(1000))
name=Field(Unicode(50))
Let's create the database itself: Now we program the simplest guest book. To do this, edit test1 / app.py as follows:
$ /usr/local/nagare/bin/nagare-admin create-db test1
2010-03-17 18:08:22,895 INFO sqlalchemy.engine.base.Engine.0x...ac6c PRAGMA table_info("test1_models_guestbookrecord")
2010-03-17 18:08:22,900 INFO sqlalchemy.engine.base.Engine.0x...ac6c ()
2010-03-17 18:08:22,934 INFO sqlalchemy.engine.base.Engine.0x...ac6c
CREATE TABLE test1_models_guestbookrecord (
id INTEGER NOT NULL,
text VARCHAR(1000),
name VARCHAR(50),
PRIMARY KEY (id)
)
2010-03-17 18:08:22,971 INFO sqlalchemy.engine.base.Engine.0x...ac6c ()
2010-03-17 18:08:22,982 INFO sqlalchemy.engine.base.Engine.0x...ac6c COMMIT
from __future__ import with_statement import os from nagare import presentation, var # Импортируем структуру таблиц from models import * class Test1(object): def __init__(self): self.name=var.Var("") # В эти переменные будут помещаться значения полей формы self.text=var.Var("") # # Метод для добавления записи в БД def add_rec(self): nr=GuestBookRecord() nr.name=self.name() nr.text=self.text() session.save(nr) @presentation.render_for(Test1) def render(self, h, *args): # Заголовок h<
Launching the application with the command: At the address \ http: //127.0.0.1: 8080 / test1 we will find a premium guest book, in the style of "Hello from the 90s." The above example does not reveal 10% of the capabilities of Nagare and is given only as an example. Next time I'll show you how to use Nagare to create more complex applications.$ /usr/local/nagare/bin/nagare-admin serve test1