目次
文書の過去の版を表示しています。
Emmett - 非同期 Web アプリケーション フレームワーク
Dr. Emmett Brown - Incredible Characters Wiki
File:Doc - BackTTFuture 067Pyxurz.jpg - Incredible Characters Wiki より
本家: Emmett - the web framework for inventors
ソースコード: emmett-framework/emmett: The web framework for inventors
Emmett チュートリアル
公式: Tutorial | Emmett framework
$ git clone --depth 1 https://github.com/emmett-framework/emmett.git
Cloning into 'emmett'... remote: Enumerating objects: 231, done. remote: Counting objects: 100% (231/231), done. remote: Compressing objects: 100% (217/217), done. remote: Total 231 (delta 7), reused 112 (delta 4), pack-reused 0 Receiving objects: 100% (231/231), 690.73 KiB | 5.48 MiB/s, done. Resolving deltas: 100% (7/7), done.
$ cd emmett/examples/bloggy $ python -m venv .venv && . .venv/bin/activate && pip install -U pip
Requirement already satisfied: pip in ./.venv/lib64/python3.12/site-packages (23.3.2) Collecting pip Downloading pip-24.1-py3-none-any.whl.metadata (3.6 kB) Downloading pip-24.1-py3-none-any.whl (1.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 9.0 MB/s eta 0:00:00 Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 23.3.2 Uninstalling pip-23.3.2: Successfully uninstalled pip-23.3.2 Successfully installed pip-24.1
(.venv) $ pip install emmett
Collecting emmett Downloading emmett-2.5.11-py3-none-any.whl.metadata (4.8 kB) Collecting click>=6.0 (from emmett) Downloading click-8.1.7-py3-none-any.whl.metadata (3.0 kB) Collecting emmett-crypto<0.7,>=0.6 (from emmett) Downloading emmett_crypto-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.9 kB) Collecting granian<1.5.0,>=1.4.1 (from emmett) Downloading granian-1.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (15 kB) Collecting pendulum<3.1.0,>=3.0.0 (from emmett) Downloading pendulum-3.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.9 kB) Collecting pyDAL==17.3 (from emmett) Downloading pyDAL-17.3-py2.py3-none-any.whl.metadata (1.4 kB) Collecting python-rapidjson<2.0,>=1.14 (from emmett) Downloading python_rapidjson-1.17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (21 kB) Collecting pyyaml<7.0,>=6.0 (from emmett) Downloading PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (2.1 kB) Collecting renoir<2.0,>=1.6 (from emmett) Downloading renoir-1.7.1-py3-none-any.whl.metadata (2.0 kB) Collecting severus<2.0,>=1.1 (from emmett) Downloading severus-1.3.0-py3-none-any.whl.metadata (1.9 kB) Collecting uvloop>=0.18.0 (from granian<1.5.0,>=1.4.1->emmett) Downloading uvloop-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.9 kB) Collecting python-dateutil>=2.6 (from pendulum<3.1.0,>=3.0.0->emmett) Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB) Collecting tzdata>=2020.1 (from pendulum<3.1.0,>=3.0.0->emmett) Downloading tzdata-2024.1-py2.py3-none-any.whl.metadata (1.4 kB) Collecting time-machine>=2.6.0 (from pendulum<3.1.0,>=3.0.0->emmett) Downloading time_machine-2.14.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (21 kB) Collecting six>=1.5 (from python-dateutil>=2.6->pendulum<3.1.0,>=3.0.0->emmett) Downloading six-1.16.0-py2.py3-none-any.whl.metadata (1.8 kB) Downloading emmett-2.5.11-py3-none-any.whl (450 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 450.5/450.5 kB 6.7 MB/s eta 0:00:00 Downloading pyDAL-17.3-py2.py3-none-any.whl (169 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 169.6/169.6 kB 9.3 MB/s eta 0:00:00 Downloading click-8.1.7-py3-none-any.whl (97 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 8.6 MB/s eta 0:00:00 Downloading emmett_crypto-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (219 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 219.0/219.0 kB 11.7 MB/s eta 0:00:00 Downloading granian-1.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.4/3.4 MB 10.9 MB/s eta 0:00:00 Downloading pendulum-3.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (384 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.9/384.9 kB 10.5 MB/s eta 0:00:00 Downloading python_rapidjson-1.17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 11.1 MB/s eta 0:00:00 Downloading PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (724 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 725.0/725.0 kB 11.1 MB/s eta 0:00:00 Downloading renoir-1.7.1-py3-none-any.whl (30 kB) Downloading severus-1.3.0-py3-none-any.whl (12 kB) Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 229.9/229.9 kB 10.6 MB/s eta 0:00:00 Downloading time_machine-2.14.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33 kB) Downloading tzdata-2024.1-py2.py3-none-any.whl (345 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 345.4/345.4 kB 11.4 MB/s eta 0:00:00 Downloading uvloop-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 11.3 MB/s eta 0:00:00 Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) Installing collected packages: pyDAL, uvloop, tzdata, six, renoir, pyyaml, python-rapidjson, emmett-crypto, click, severus, python-dateutil, granian, time-machine, pendulum, emmett Successfully installed click-8.1.7 emmett-2.5.11 emmett-crypto-0.6.0 granian-1.4.4 pendulum-3.0.0 pyDAL-17.3 python-dateutil-2.9.0.post0 python-rapidjson-1.17 pyyaml-6.0.1 renoir-1.7.1 severus-1.3.0 six-1.16.0 time-machine-2.14.1 tzdata-2024.1 uvloop-0.19.0
(.venv) $ mkdir -p databases (.venv) $ emmett migrations up
> Performing upgrades against sqlite://dummy.db > Performing upgrade: <base> -> 9d6518b3cdc2 (head), First migration > Adding revision 9d6518b3cdc2 to schema > Succesfully upgraded to revision 9d6518b3cdc2: First migration
(.venv) $ emmett setup (.venv) $ emmett develop
> Starting Emmett development server on app app > Emmett application app running on http://127.0.0.1:8000 (press CTRL+C to quit) > Restarting (stat mode) > WARNING in ext [/home/tomoyan/my_projects/emmett/examples/bloggy/.venv/lib64/python3.12/site-packages/emmett/tools/auth/ext.py:175]: No mailer seems to be configured. The auth features requiring mailer won't work. [INFO] Starting granian (main PID: 108684) [INFO] Listening at: http://127.0.0.1:8000 [INFO] Spawning worker-1 with pid: 108685 [INFO] Started worker-1 [INFO] Started worker-1 runtime-1
http://localhost:8000 にアクセスする🤔
プロジェクトの作成
$ mkdir -p emmett_proj && cd emmett_proj $ python -m venv .venv && . .venv/bin/activate && pip install -U pip
Requirement already satisfied: pip in ./.venv/lib64/python3.12/site-packages (23.3.2) Collecting pip Downloading pip-24.1-py3-none-any.whl.metadata (3.6 kB) Downloading pip-24.1-py3-none-any.whl (1.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 8.8 MB/s eta 0:00:00 Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 23.3.2 Uninstalling pip-23.3.2: Successfully uninstalled pip-23.3.2 Successfully installed pip-24.1
(.venv) $ pip install emmett
Collecting emmett Downloading emmett-2.5.11-py3-none-any.whl.metadata (4.8 kB) Collecting click>=6.0 (from emmett) Downloading click-8.1.7-py3-none-any.whl.metadata (3.0 kB) Collecting emmett-crypto<0.7,>=0.6 (from emmett) Downloading emmett_crypto-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.9 kB) Collecting granian<1.5.0,>=1.4.1 (from emmett) Downloading granian-1.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (15 kB) Collecting pendulum<3.1.0,>=3.0.0 (from emmett) Downloading pendulum-3.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.9 kB) Collecting pyDAL==17.3 (from emmett) Downloading pyDAL-17.3-py2.py3-none-any.whl.metadata (1.4 kB) Collecting python-rapidjson<2.0,>=1.14 (from emmett) Downloading python_rapidjson-1.17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (21 kB) Collecting pyyaml<7.0,>=6.0 (from emmett) Downloading PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (2.1 kB) Collecting renoir<2.0,>=1.6 (from emmett) Downloading renoir-1.7.1-py3-none-any.whl.metadata (2.0 kB) Collecting severus<2.0,>=1.1 (from emmett) Downloading severus-1.3.0-py3-none-any.whl.metadata (1.9 kB) Collecting uvloop>=0.18.0 (from granian<1.5.0,>=1.4.1->emmett) Downloading uvloop-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.9 kB) Collecting python-dateutil>=2.6 (from pendulum<3.1.0,>=3.0.0->emmett) Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB) Collecting tzdata>=2020.1 (from pendulum<3.1.0,>=3.0.0->emmett) Downloading tzdata-2024.1-py2.py3-none-any.whl.metadata (1.4 kB) Collecting time-machine>=2.6.0 (from pendulum<3.1.0,>=3.0.0->emmett) Downloading time_machine-2.14.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (21 kB) Collecting six>=1.5 (from python-dateutil>=2.6->pendulum<3.1.0,>=3.0.0->emmett) Downloading six-1.16.0-py2.py3-none-any.whl.metadata (1.8 kB) Downloading emmett-2.5.11-py3-none-any.whl (450 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 450.5/450.5 kB 7.2 MB/s eta 0:00:00 Downloading pyDAL-17.3-py2.py3-none-any.whl (169 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 169.6/169.6 kB 9.3 MB/s eta 0:00:00 Downloading click-8.1.7-py3-none-any.whl (97 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 9.1 MB/s eta 0:00:00 Downloading emmett_crypto-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (219 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 219.0/219.0 kB 9.4 MB/s eta 0:00:00 Downloading granian-1.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.4/3.4 MB 5.8 MB/s eta 0:00:00 Downloading pendulum-3.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (384 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.9/384.9 kB 10.6 MB/s eta 0:00:00 Downloading python_rapidjson-1.17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 11.3 MB/s eta 0:00:00 Downloading PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (724 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 725.0/725.0 kB 10.0 MB/s eta 0:00:00 Downloading renoir-1.7.1-py3-none-any.whl (30 kB) Downloading severus-1.3.0-py3-none-any.whl (12 kB) Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 229.9/229.9 kB 11.7 MB/s eta 0:00:00 Downloading time_machine-2.14.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33 kB) Downloading tzdata-2024.1-py2.py3-none-any.whl (345 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 345.4/345.4 kB 10.9 MB/s eta 0:00:00 Downloading uvloop-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 11.3 MB/s eta 0:00:00 Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) Installing collected packages: pyDAL, uvloop, tzdata, six, renoir, pyyaml, python-rapidjson, emmett-crypto, click, severus, python-dateutil, granian, time-machine, pendulum, emmett Successfully installed click-8.1.7 emmett-2.5.11 emmett-crypto-0.6.0 granian-1.4.4 pendulum-3.0.0 pyDAL-17.3 python-dateutil-2.9.0.post0 python-rapidjson-1.17 pyyaml-6.0.1 renoir-1.7.1 severus-1.3.0 six-1.16.0 time-machine-2.14.1 tzdata-2024.1 uvloop-0.19.0
技術情報
Emmett は ASGI ではなく RSGI で動作する...🤔
Granian HTTPサーバーはASGI 代替プロトコルRSGI(Rust Server Gateway Interface)を実装してて、Emmett 2.5 で使用されるデフォルトのプロトコル、uvicorn への依存は削除されてる🤔
コードを変更することなく、Webアプリケーションのパフォーマンスが約 50% 向上してる😍
Upgrading to newer releases | Emmett framework
RSGI Specification - granian/docs/spec/RSGI.md at master · emmett-framework/granian
データベース URL に指定可能な文字列の例...🤔
公式: Welcome to pyDAL’s API documentation! — pyDAL 17.11 documentation
Emmett は内部で web2py Web フレームワーク 由来の pyDAL を利用するので、そのドキュメントに記載がある😉
'sqlite://test.db' 'spatialite://test.db' 'sqlite:memory' 'spatialite:memory' 'jdbc:sqlite://test.db' 'mysql://root:none@localhost/test' 'postgres://mdipierro:password@localhost/test' 'postgres:psycopg2://mdipierro:password@localhost/test' 'postgres:pg8000://mdipierro:password@localhost/test' 'jdbc:postgres://mdipierro:none@localhost/test' 'mssql://web2py:none@A64X2/web2py_test' 'mssql2://web2py:none@A64X2/web2py_test' # alternate mappings 'mssql3://web2py:none@A64X2/web2py_test' # better pagination (requires >= 2005) 'mssql4://web2py:none@A64X2/web2py_test' # best pagination (requires >= 2012) 'oracle://username:password@database' 'firebird://user:password@server:3050/database' 'db2:ibm_db_dbi://DSN=dsn;UID=user;PWD=pass' 'db2:pyodbc://driver=DB2;hostname=host;database=database;uid=user;pwd=password;port=port' 'firebird://username:password@hostname/database' 'firebird_embedded://username:password@c://path' 'informix://user:password@server:3050/database' 'informixu://user:password@server:3050/database' # unicode informix 'ingres://database' # or use an ODBC connection string, e.g. 'ingres://dsn=dsn_name' 'google:datastore' # for google app engine datastore (uses ndb by default) 'google:sql' # for google app engine with sql (mysql compatible) 'teradata://DSN=dsn;UID=user;PWD=pass; DATABASE=database' # experimental 'imap://user:password@server:port' # experimental 'mongodb://user:password@server:port/database' # experimental
トラブルシューティング
RuntimeError: No driver of supported ones ('psycopg2', 'pg8000') is available
PostgreSQL に接続する際に以下の内容のようなエラーが発生する…😱
File "/home/tomoyan/my_projects/emmett_proj/.venv/lib64/python3.12/site-packages/pydal/adapters/base.py", line 367, in __init__ super(SQLAdapter, self).__init__(*args, **kwargs) File "/home/tomoyan/my_projects/emmett_proj/.venv/lib64/python3.12/site-packages/pydal/adapters/base.py", line 49, in __init__ self.find_driver() File "/home/tomoyan/my_projects/emmett_proj/.venv/lib64/python3.12/site-packages/pydal/adapters/base.py", line 98, in find_driver raise RuntimeError( RuntimeError: No driver of supported ones ('psycopg2', 'pg8000') is available
PostgreSQL ドライバをインストールする必要がある🤔
(.venv) $ pip install psycopg2
Collecting psycopg2 Downloading psycopg2-2.9.9.tar.gz (384 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.9/384.9 kB 27.4 kB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: psycopg2 Building wheel for psycopg2 (pyproject.toml) ... done Created wheel for psycopg2: filename=psycopg2-2.9.9-cp312-cp312-linux_x86_64.whl size=170256 sha256=2e90e7bb4e1ff74e850d424bfbc9bf7c017ffbb1c37b42d79b550d13f29af782 Stored in directory: /home/tomoyan/.cache/pip/wheels/ff/ac/80/7ccec163e3d05ae2112311b895132409b9abfd7e0c1c6b5183 Successfully built psycopg2 Installing collected packages: psycopg2 Successfully installed psycopg2-2.9.9
参考文献
Elegant Web Development With Emmett In Python | by Emma Boudreau | Towards Data Science
The Ultimate Guide to Python Development Frameworks 2021 | by Pangea.ai | Medium