ながれ-フレームワークの使用例

以前の投稿で、私はNagareSmalltalkCLで他の追随を許し ませんが )Python Webフレームワークを紹介しました。 その投稿はやや面倒で、むしろテクノロジーの実際の機能よりも私の熱意の程度を反映しています。 今日は、もう少し実用的な例を示します。



やり直しましょう。 まず、 Stackless Pythonが必要です 。 FreeBSDにインストールするには(以下、このシステムをベースとして使用します。Linuxには違いはありません。MacOSとWindowsには機能がありますが、重要ではありません)、配布キットをダウンロードする必要があります。



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









開梱してください:

$ tar -xvjf stackless-264-export.tar.bz2







configure(/ usr / local / stacklessにインストールします)

$ cd stackless-2.6.4

$ ./configure --prefix=/usr/local/stackless







コンパイルとインストール:

$ make

$ sudo make install









実際には、portsシステムを介してstackless-pythonをインストールするのが最も正しいでしょうが、何らかの理由で欠落しています。



ここで、 virtualenvをダウンロードして解凍する必要があります。

$ fetch \http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.5.tar.gz

$ tar -xvzf virtualenv-1.4.5.tar.gz







次に、ながれ(/ usr / local / nagare)が機能する仮想「サンドボックス」を作成します。

$ 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]'









すべて、Nagareがインストールされ、準備完了です。

アプリケーションを作成しましょう:

$ /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ディレクトリが作成されます。



./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



setup.pyファイルには、ライセンス、作成者の名前、電子メールなどのすべてのサービス情報が含まれています。テストアプリケーションの場合は、空白のままでかまいません。



conf / test1.cfgファイルで、デバッグモードを有効にし、データベースを接続します(デフォルトではsqlite)。 修正後、次のようになります。



[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









デフォルトでは、アプリケーションは\ http://127.0.0.1:8080 / test1で利用できます。

開発に進みます。

まず、データベーステーブルを設定します。 これを行うには、test1 / models.pyファイルを編集し、テーブル定義を追加します(詳細に興味がある場合は、 Elixirのドキュメントを参照します)。 その結果、ファイルは次のようになります。



 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))









データベース自体を作成しましょう。

$ /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









次に、最も単純なゲストブックをプログラムします。 これを行うには、次のようにtest1 / app.pyを編集します。



 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









次のコマンドを使用してアプリケーションを実行します。

$ /usr/local/nagare/bin/nagare-admin serve test1









アドレス\ http://127.0.0.1:8080 / test1には、「90年代からの挨拶」のスタイルのプレミアムゲストブックがあります。



上記の例では、ながれの能力の10%は明らかにされておらず、例としてのみ提供されています。 次回は、もっと複雑なアプリケーションを作成するためにNagareを使用する方法を紹介します。



All Articles