PyCon JP 2011 Sprints に参加してきました。
午前中だけでしたが、PyCon JP Sprintsに参加しました。
間違いがありましたら、適宜ご指摘願います。
SprintsではPyramid を動かしました。
流れは下記
- 1. 環境構築
- 2. ページの作成
- 3. jinja2の利用
前提条件
- virtualenv, virtualenvwrapper, pip を使う方法 - Ian Lewisよりvirtualenv環境の構築
1. 環境構築
参考:Installing Pyramid
1. まず、Pyramid用のプロジェクトフォルダを作成します。
$ virtualenv --no-site-packages pyramid New python executable in pyramid/bin/python Installing setuptools............done. Installing pip...............done.
2. プロジェクトフォルダへPyramidをインストールします。
$ cd pyramid $ bin/easy_install pyramid Searching for pyramid Reading http://pypi.python.org/simple/pyramid/ Reading http://pylonsproject.org Reading http://docs.pylonsproject.org ... Adding ordereddict 1.1 to easy-install.pth file Installed /****/pyramid/lib/python2.6/(本当は一行) site-packages/ordereddict-1.1-py2.6.egg Finished processing dependencies for pyramid
※この段階でもはや動作します。
参考:Creating Your First Pyramid Application
$ vim helloworld.py from paste.httpserver import serve from pyramid.configuration import Configurator from pyramid.response import Response def hello_world(request): return Response('Hello %(name)s!' % request.matchdict) if __name__ == '__main__': config = Configurator() config.add_route('hello', '/hello/{name}') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app() serve(app, host='0.0.0.0') $ bin/python helloworld.py helloworld.py:2: DeprecationWarning: Configurator:(本当は一行) pyramid.configuration.Configurator is deprecated as of(本当は一行) Pyramid 1.0. Use``pyramid.config.Configurator`` with(本当は一行) ``autocommit=True`` instead. from pyramid.configuration import Configurator serving on 0.0.0.0:8080 view at http://127.0.0.1:8080
となり、http://hostname:8080/hello/worldにて動作するようになる。
上記プログラムは単純にhelloの次に入力文字をhelloとくっつけて表示するもの。
3. pyramid_starterを用いたプロジェクトの作成
$ bin/paster create -t pyramid_starter Selected and implied templates: pyramid#pyramid_starter pyramid starter project Enter project name: Sample Variables: egg: Sample package: sample project: Sample Creating template pyramid_starter Creating directory ./Sample ... Copying setup.py_tmpl to ./Sample/setup.py Welcome to Pyramid. Sorry for the convenience. Running /****/pyramid/bin/python setup.py egg_info
4. 開発者機能をインストールする
$ cd Sample $ ../bin/python setup.py develop running develop running egg_info writing requirements to Sample.egg-info/requires.txt ... Using /****/pyramid/lib/python2.6/site-packages/ordereddict-1.1-py2.6.egg Finished processing dependencies for Sample==0.0
2. ページの作成
今回は、/helloworld にページを作成します。
1. ルーティングを記載する
Sample/sample/__init__.py に記載します。
return config.make_wsgi_app() の上あたりに記載します。
$ vim sample/__init__.py ... config.add_route('helloworld', 'helloworld') config.add_view('sample.views.helloworld', route_name='helloworld', renderer='sample:templates/helloworld.pt') ...
参考:Routing Examples - Example 1
2. メソッドを記載する
pyramid/Sample/sample/views.py に記載します。
$ vim sample/views.py ... def helloworld(request): return {'name':'jptomo'}
3. ページを作成する
pyramid/Sample/sample/temples/helloworld.pt にファイルを作成し、
下記の内容を記載します。
$ vim sample/temples/helloworld.pt hello ${name}
4. paster server を立てる
$ ../bin/paster serve development.ini --reload & Starting subprocess with file monitor Starting server in PID 1766. 2011-08-28 16:45:28,535 DEBUG [paste.httpserver.ThreadPool](本当は一行) [worker 0] Started new worker -1231787152: Initial worker pool ...
5. アクセス
http://localhost:6543にアクセスします。
3. jinja2の利用
参考:pyramid_jinja2
1. pyramid_jinja2のインストール
$ ../bin/easy_install pyramid_jinja2 Searching for pyramid-jinja2 Best match: pyramid-jinja2 1.1 Processing pyramid_jinja2-1.1-py2.6.egg pyramid-jinja2 1.1 is already the active version in easy-install.pth Using /****/pyramid/lib/python2.6/site-packages/pyramid_jinja2-1.1-py2.6.egg Processing dependencies for pyramid-jinja2 Finished processing dependencies for pyramid-jinja2
2. 読み込み設定
Sample/sample/__init__.py を修正します。
config = Configurator...の直下あたりに記載してください。
またテンプレートの拡張子も変更する必要があり、
そのため、上記のファイルの .pt を .jinja2 に修正してください。
$ vim sample/__init__.py ... config = Configurator(root_factory=Root, settings=settings) config.include('pyramid_jinja2') ... renderer='sample:templates/helloworld.jinja2') ...
3. テンプレート修正
jinja2を利用するにはテンプレートの拡張子を
jinja2にしないといけません。
また記法も修正しなければいけません。
$ mv sample/templates/helloworld.pt sample/templates/helloworld.jinja2 $ vim sample/templates/helloworld.jinja2 hello {{ name }}