📜 ⬆️ ⬇️

Nagare - an example of using the framework

In the last post, I presented Nagare , a revolutionary (albeit with Smalltalk and CL counterparts) Python web framework. That post was somewhat muddled 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 . For installation on FreeBSD (hereinafter, I will use this system as a base, there should be no differences for Linux, there are features for MacOS and Windows, but they are not critical) you need to download the distribution:

$ fetch \http://www.stackless.com/binaries/stackless-264-export.tar.bz2

')
unpack it:
$ tar -xvjf stackless-264-export.tar.bz2

configure (we will install in / usr / local / stackless)
$ cd stackless-2.6.4
$ ./configure --prefix=/usr/local/stackless

compile and install:
$ make
$ sudo make install


In fact, it would be most appropriate 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 :
$ fetch \http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.5.tar.gz
$ tar -xvzf virtualenv-1.4.5.tar.gz

then create a virtual sandbox in which Nagare will work (/ usr / local / nagare):
$ cd virtualenv-1.4.5
$ sudo /usr/local/stackless/bin/python ./virtualenv.py /usr/local/nagare


Now, you need to install Nagare itself:
sudo /usr/local/nagare/bin/easy_install 'nagare[full]'


That's all, Nagare is installed and ready to go.
Create an application:
$ /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


A 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 any service information such as a license, the name of the author, his email, etc., for a test application, you can leave it blank, and it will work this way.

In the conf / test1.cfg file, enable debug mode and enable the database (by default - sqlite). After corrections, it 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


Now run the command:
$ sudo /usr/local/nagare/bin/python ./setup.py develop


Everything, the application is registered, you can test it:
$ /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


By default, the application will be available at \ http: //127.0.0.1: 8080 / test1.
Let's start the development.
First we set the database tables. To do this, edit the test1 / models.py file, adding table definitions (interested in details, refer to the Elixir documentation). As a result, the file will look like this:

 from elixir import * from sqlalchemy import MetaData __metadata__ = MetaData() class GuestBookRecord(Entity): text=Field(Unicode(1000)) name=Field(Unicode(50)) 
from elixir import * from sqlalchemy import MetaData __metadata__ = MetaData() class GuestBookRecord(Entity): text=Field(Unicode(1000)) name=Field(Unicode(50))


Create the database itself:
$ /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


Now we will program the elementary guest book. To do this, we edit the test1 / app.py file as follows:

 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<<h.h1("Guest book") #       with h.form: with h.table: with h.tr: with h.td: h<<"Name:" with h.td: h<<h.input(type="text", value=self.name()).action(lambda v: self.name(v)) with h.tr: with h.td(valign="top"): h<<"Text:" with h.td: with h.textarea().action(lambda v: self.text(v)): pass with h.tr: with h.td(colspan=2): h<<h.input(type="submit", value="Add").action(self.add_rec) h<<h.hr #    : with h.table: recs=GuestBookRecord.query.all() for r in recs: with h.tr: with h.td(valign="top", align="right"): h<<hb(r.name)<<":" with h.td: h<<r.text return h.root # --------------------------------------------------------------- app = Test1 
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<<h.h1("Guest book") # with h.form: with h.table: with h.tr: with h.td: h<<"Name:" with h.td: h<<h.input(type="text", value=self.name()).action(lambda v: self.name(v)) with h.tr: with h.td(valign="top"): h<<"Text:" with h.td: with h.textarea().action(lambda v: self.text(v)): pass with h.tr: with h.td(colspan=2): h<<h.input(type="submit", value="Add").action(self.add_rec) h<<h.hr # : with h.table: recs=GuestBookRecord.query.all() for r in recs: with h.tr: with h.td(valign="top", align="right"): h<<hb(r.name)<<":" with h.td: h<<r.text return h.root # --------------------------------------------------------------- app = Test1


Running the application with the command:
$ /usr/local/nagare/bin/nagare-admin serve test1


To the address \ http: //127.0.0.1: 8080 / test1 we will find a premium guestbook, in the style of “hello from the 90s”.

The above example does not reveal even 10% of the capabilities of Nagare and is given only as an example. Next time, I'll show you how to create more complex applications with Nagare.

Source: https://habr.com/ru/post/87908/


All Articles