GoogleColaboratoryでfastapiを実行する

スポンサーリンク

GoogleColaboratoryでfastapiを実行してみます。

fastapiは、PythonでRESTful API を構築するためのWeb フレームワークである。

■今回の環境(Python)

今回のPythonは、バージョン3.9.16を用いる。(なお、Google Colaboratory(Google Colab)を使用。)

■fastapiを実行するための準備

では、GoogleColaboratoryでfastapiを実行してみますので、まずはそのための準備を行います。

■コード

  1. !pip install colabcode
  2. !pip install fastapi

今回は、Google Colabでcodeserverを実行できる「colabcode」と、fastapiをインストールします。※code-serverはブラウザからアクセスして利用できるVSCodeのサーバアプリケーション。

警告

実行中のコードは許可されていない可能性があるため、今後の Colab の利用を制限させていただく場合があります。よくある質問で指定されている禁止行為をご確認ください。

セルにコードを記述し、実行しようとすると、上記のメッセージが表示されます。今回は検証のため「続行」ボタンをクリックします。

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting colabcode
  Downloading colabcode-0.3.0-py3-none-any.whl (5.0 kB)
Collecting uvicorn==0.13.1
  Downloading uvicorn-0.13.1-py3-none-any.whl (45 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 45.5/45.5 kB 1.4 MB/s eta 0:00:00
Collecting pyngrok>=5.0.0
  Downloading pyngrok-6.0.0.tar.gz (681 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 681.2/681.2 kB 17.9 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting jupyterlab==3.0.7
  Downloading jupyterlab-3.0.7-py3-none-any.whl (8.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.3/8.3 MB 49.1 MB/s eta 0:00:00
Collecting nest-asyncio==1.4.3
  Downloading nest_asyncio-1.4.3-py3-none-any.whl (5.3 kB)
Requirement already satisfied: jupyter-server~=1.2 in /usr/local/lib/python3.10/dist-packages (from jupyterlab==3.0.7->colabcode) (1.24.0)
Collecting jupyterlab-server~=2.0
  Downloading jupyterlab_server-2.22.1-py3-none-any.whl (57 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.0/57.0 kB 4.0 MB/s eta 0:00:00
Collecting nbclassic~=0.2
  Downloading nbclassic-0.5.6-py3-none-any.whl (10.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 38.6 MB/s eta 0:00:00
Requirement already satisfied: jinja2>=2.10 in /usr/local/lib/python3.10/dist-packages (from jupyterlab==3.0.7->colabcode) (3.1.2)
Requirement already satisfied: ipython in /usr/local/lib/python3.10/dist-packages (from jupyterlab==3.0.7->colabcode) (7.34.0)
Requirement already satisfied: tornado>=6.1.0 in /usr/local/lib/python3.10/dist-packages (from jupyterlab==3.0.7->colabcode) (6.2)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from jupyterlab==3.0.7->colabcode) (23.1)
Requirement already satisfied: jupyter-core in /usr/local/lib/python3.10/dist-packages (from jupyterlab==3.0.7->colabcode) (5.3.0)
Collecting click==7.*
  Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.8/82.8 kB 5.9 MB/s eta 0:00:00
Collecting h11>=0.8
  Downloading h11-0.14.0-py3-none-any.whl (58 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 3.1 MB/s eta 0:00:00
Requirement already satisfied: PyYAML in /usr/local/lib/python3.10/dist-packages (from pyngrok>=5.0.0->colabcode) (6.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2>=2.10->jupyterlab==3.0.7->colabcode) (2.1.2)
Requirement already satisfied: traitlets>=5.1 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (5.7.1)
Requirement already satisfied: anyio<4,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (3.6.2)
Requirement already satisfied: Send2Trash in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.8.0)
Requirement already satisfied: pyzmq>=17 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (23.2.1)
Requirement already satisfied: prometheus-client in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.16.0)
Requirement already satisfied: websocket-client in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.5.1)
Requirement already satisfied: argon2-cffi in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (21.3.0)
Requirement already satisfied: jupyter-client>=6.1.12 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (6.1.12)
Requirement already satisfied: terminado>=0.8.3 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.17.1)
Requirement already satisfied: nbformat>=5.2.0 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (5.8.0)
Requirement already satisfied: nbconvert>=6.4.4 in /usr/local/lib/python3.10/dist-packages (from jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (6.5.4)
Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.10/dist-packages (from jupyter-core->jupyterlab==3.0.7->colabcode) (3.3.0)
Collecting json5>=0.9.0
  Downloading json5-0.9.11-py2.py3-none-any.whl (19 kB)
Requirement already satisfied: babel>=2.10 in /usr/local/lib/python3.10/dist-packages (from jupyterlab-server~=2.0->jupyterlab==3.0.7->colabcode) (2.12.1)
Collecting requests>=2.28
  Downloading requests-2.30.0-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.5/62.5 kB 3.5 MB/s eta 0:00:00
Collecting jsonschema>=4.17.3
  Downloading jsonschema-4.17.3-py3-none-any.whl (90 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.4/90.4 kB 4.8 MB/s eta 0:00:00
Collecting notebook-shim>=0.2.3
  Downloading notebook_shim-0.2.3-py3-none-any.whl (13 kB)
Requirement already satisfied: ipykernel in /usr/local/lib/python3.10/dist-packages (from nbclassic~=0.2->jupyterlab==3.0.7->colabcode) (5.5.6)
Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.10/dist-packages (from nbclassic~=0.2->jupyterlab==3.0.7->colabcode) (0.2.0)
Collecting nbclassic~=0.2
  Downloading nbclassic-0.5.5-py3-none-any.whl (10.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 40.8 MB/s eta 0:00:00
  Downloading nbclassic-0.5.4-py3-none-any.whl (10.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 14.1 MB/s eta 0:00:00
  Downloading nbclassic-0.5.3-py3-none-any.whl (10.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 24.1 MB/s eta 0:00:00
  Downloading nbclassic-0.5.2-py3-none-any.whl (10.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 59.3 MB/s eta 0:00:00
  Downloading nbclassic-0.5.1-py3-none-any.whl (10.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 41.9 MB/s eta 0:00:00
  Downloading nbclassic-0.4.8-py3-none-any.whl (9.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.8/9.8 MB 10.7 MB/s eta 0:00:00
  Downloading nbclassic-0.4.7-py3-none-any.whl (9.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.8/9.8 MB 46.6 MB/s eta 0:00:00
  Downloading nbclassic-0.4.6-py3-none-any.whl (9.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.8/9.8 MB 55.1 MB/s eta 0:00:00
  Downloading nbclassic-0.4.5-py3-none-any.whl (9.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.8/9.8 MB 63.4 MB/s eta 0:00:00
  Downloading nbclassic-0.4.4-py3-none-any.whl (9.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.8/9.8 MB 63.1 MB/s eta 0:00:00
  Downloading nbclassic-0.4.3-py3-none-any.whl (9.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.7/9.7 MB 50.2 MB/s eta 0:00:00
  Downloading nbclassic-0.4.2-py3-none-any.whl (9.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.7/9.7 MB 43.9 MB/s eta 0:00:00
  Downloading nbclassic-0.4.0-py3-none-any.whl (9.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.7/9.7 MB 64.0 MB/s eta 0:00:00
  Downloading nbclassic-0.3.7-py3-none-any.whl (13 kB)
Requirement already satisfied: notebook<7 in /usr/local/lib/python3.10/dist-packages (from nbclassic~=0.2->jupyterlab==3.0.7->colabcode) (6.4.8)
Collecting jedi>=0.16
  Downloading jedi-0.18.2-py2.py3-none-any.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 54.1 MB/s eta 0:00:00
Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (0.1.6)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (0.7.5)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (3.0.38)
Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (4.8.0)
Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (0.2.0)
Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (2.14.0)
Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (4.4.2)
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from ipython->jupyterlab==3.0.7->colabcode) (67.7.2)
Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.1.0->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.3.0)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.1.0->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (3.4)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython->jupyterlab==3.0.7->colabcode) (0.8.3)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=4.17.3->jupyterlab-server~=2.0->jupyterlab==3.0.7->colabcode) (0.19.3)
Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=4.17.3->jupyterlab-server~=2.0->jupyterlab==3.0.7->colabcode) (23.1.0)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.10/dist-packages (from jupyter-client>=6.1.12->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (2.8.2)
Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.4)
Requirement already satisfied: defusedxml in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.7.1)
Requirement already satisfied: nbclient>=0.5.0 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.7.4)
Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.8.4)
Requirement already satisfied: tinycss2 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.2.1)
Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.5.0)
Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (4.9.2)
Requirement already satisfied: jupyterlab-pygments in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.2.2)
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (4.11.2)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (6.0.0)
Requirement already satisfied: fastjsonschema in /usr/local/lib/python3.10/dist-packages (from nbformat>=5.2.0->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (2.16.3)
Collecting notebook<7 Downloading notebook-6.5.4-py3-none-any.whl (529 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 529.8/529.8 kB 29.8 MB/s eta 0:00:00 Downloading notebook-6.5.3-py3-none-any.whl (529 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 529.7/529.7 kB 31.9 MB/s eta 0:00:00 Downloading notebook-6.5.2-py3-none-any.whl (439 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 439.1/439.1 kB 24.5 MB/s eta 0:00:00 Downloading notebook-6.5.1-py3-none-any.whl (439 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 439.1/439.1 kB 32.4 MB/s eta 0:00:00 Downloading notebook-6.4.12-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 17.2 MB/s eta 0:00:00 Downloading notebook-6.4.11-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 58.6 MB/s eta 0:00:00 Downloading notebook-6.4.10-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 39.1 MB/s eta 0:00:00 Downloading notebook-6.4.9-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 56.1 MB/s eta 0:00:00 Downloading notebook-6.4.7-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 50.2 MB/s eta 0:00:00 Downloading notebook-6.4.6-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 46.9 MB/s eta 0:00:00 Downloading notebook-6.4.5-py3-none-any.whl (9.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 63.6 MB/s eta 0:00:00 Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython->jupyterlab==3.0.7->colabcode) (0.7.0)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython->jupyterlab==3.0.7->colabcode) (0.2.6)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28->jupyterlab-server~=2.0->jupyterlab==3.0.7->colabcode) (2022.12.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28->jupyterlab-server~=2.0->jupyterlab==3.0.7->colabcode) (1.26.15)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28->jupyterlab-server~=2.0->jupyterlab==3.0.7->colabcode) (2.0.12)
Requirement already satisfied: argon2-cffi-bindings in /usr/local/lib/python3.10/dist-packages (from argon2-cffi->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (21.2.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.1->jupyter-client>=6.1.12->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.16.0)
Requirement already satisfied: cffi>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from argon2-cffi-bindings->argon2-cffi->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (1.15.1)
Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (2.4.1)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->nbconvert>=6.4.4->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (0.5.1)
Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->jupyter-server~=1.2->jupyterlab==3.0.7->colabcode) (2.21)
Building wheels for collected packages: pyngrok
  Building wheel for pyngrok (setup.py) ... done
  Created wheel for pyngrok: filename=pyngrok-6.0.0-py3-none-any.whl size=19879 sha256=c249fd400a5bf49f8bb14c6cd3025b734432496d728212fcbfea45c8da8a0ffd
  Stored in directory: /root/.cache/pip/wheels/5c/42/78/0c3d438d7f5730451a25f7ac6cbf4391759d22a67576ed7c2c
Successfully built pyngrok
Installing collected packages: json5, requests, pyngrok, nest-asyncio, jsonschema, jedi, h11, click, uvicorn, notebook, notebook-shim, jupyterlab-server, nbclassic, jupyterlab, colabcode
  Attempting uninstall: requests
    Found existing installation: requests 2.27.1
    Uninstalling requests-2.27.1:
      Successfully uninstalled requests-2.27.1
  Attempting uninstall: nest-asyncio
    Found existing installation: nest-asyncio 1.5.6
    Uninstalling nest-asyncio-1.5.6:
      Successfully uninstalled nest-asyncio-1.5.6
  Attempting uninstall: jsonschema
    Found existing installation: jsonschema 4.3.3
    Uninstalling jsonschema-4.3.3:
      Successfully uninstalled jsonschema-4.3.3
  Attempting uninstall: click
    Found existing installation: click 8.1.3
    Uninstalling click-8.1.3:
      Successfully uninstalled click-8.1.3
  Attempting uninstall: notebook
    Found existing installation: notebook 6.4.8
    Uninstalling notebook-6.4.8:
      Successfully uninstalled notebook-6.4.8
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
google-colab 1.0.0 requires notebook==6.4.8, but you have notebook 6.4.5 which is incompatible.
flask 2.2.4 requires click>=8.0, but you have click 7.1.2 which is incompatible.
Successfully installed click-7.1.2 colabcode-0.3.0 h11-0.14.0 jedi-0.18.2 json5-0.9.11 jsonschema-4.17.3 jupyterlab-3.0.7 jupyterlab-server-2.22.1 nbclassic-0.3.7 nest-asyncio-1.4.3 notebook-6.4.5 notebook-shim-0.2.3 pyngrok-6.0.0 requests-2.30.0 uvicorn-0.13.1
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting fastapi
  Downloading fastapi-0.95.1-py3-none-any.whl (56 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.0/57.0 kB 2.9 MB/s eta 0:00:00
Collecting starlette<0.27.0,>=0.26.1
  Downloading starlette-0.26.1-py3-none-any.whl (66 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.9/66.9 kB 5.0 MB/s eta 0:00:00
Requirement already satisfied: pydantic!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0,>=1.6.2 in /usr/local/lib/python3.10/dist-packages (from fastapi) (1.10.7)
Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from pydantic!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0,>=1.6.2->fastapi) (4.5.0)
Requirement already satisfied: anyio<5,>=3.4.0 in /usr/local/lib/python3.10/dist-packages (from starlette<0.27.0,>=0.26.1->fastapi) (3.6.2)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.4.0->starlette<0.27.0,>=0.26.1->fastapi) (3.4)
Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.4.0->starlette<0.27.0,>=0.26.1->fastapi) (1.3.0)
Installing collected packages: starlette, fastapi
Successfully installed fastapi-0.95.1 starlette-0.26.1

クリックすると、「ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.」というエラーが出力されたが、一旦無視します。クリック後、インストールが完了となります。

■コード

  1. from colabcode import ColabCode
  2. from fastapi import FastAPI
  3. cc = ColabCode(port=12000, code=False)
  4. app = FastAPI()
  5. @app.get(“/”)
  6. async def read_root():
  7.   return {“message”: “Subscribe to @1littlecoder”}
  8. cc.run_app(app=app)

完了後、ColabCode()を用いる。括弧内には引数,パラメータとして、code-serverを実行するポートを「port=12000」に指定、さらに「code=False」に指定し、code-serverを用いてfastapiを実行する。

セルに今回のコードを記述し実行してみます。

WARNING:pyngrok.process.ngrok:t=2023-05-07T04:13:18+0000 lvl=warn msg="ngrok config file found at legacy location, move to XDG location" xdg_path=/root/.config/ngrok/ngrok.yml legacy_path=/root/.ngrok2/ngrok.yml
INFO:     Started server process [382]
INFO:uvicorn.error:Started server process [382]
INFO:     Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:12000 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:12000 (Press CTRL+C to quit)
Public URL: NgrokTunnel: "https://******.ngrok.io" -> "http://localhost:12000"

実行すると、上記のメッセージが出力されます。メッセージ内の「Public URL: NgrokTunnel: “https://******.ngrok.io”」で記載されたNgrokTunnelのURLをクリックします。

クリックすると、JSONレスポンスが表示されることを確認できました。

今回GoogleColaboratoryでfastapiを実行してわかったことは、colabcodeというライブラリを使うと、「実行中のコードは許可されていない可能性があるため、今後の Colab の利用を制限」される場合があるので、著者は推奨しない。

コメント

タイトルとURLをコピーしました