2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Windows で FastAPI で SQL Server のデータを利用する1

Last updated at Posted at 2022-10-10

背景

会社にて、FastAPI で、SQL Server の情報を利用する必要が生じたので、試行した記録

まずは以下まで

  1. WSL
  2. Python
  3. FastAPI

WSL を使用して Windows に Linux をインストールする

install

install
wsl --install

image.png

失敗?

途中で何故かダウンロードバーが停止・・ :thinking:
10分ほど待つが変化無いので、一旦終了してやり直そうとしたところ、再起動しろとか言われるので、再起動する・・

再度 install コマンドを入れると、Helpが出る。
まさに以下
image.png

ということで、ディストリビューション確認

ディストリビューション確認
wsl --list --online

image.png

Ubuntu との違いが判らなかったけれど、まぁ、最新でいいか、と Ubuntu-20.4 を install
・・もしかしたら、常時最新版って意味だったのか?

Ubuntu-20.4 install
wsl --install -d Ubuntu-20.04

image.png

Ubuntu 初期設定

  1. User 登録
    image.png
  2. password 登録で完了
    image.png

補足

プロキシの設定

自宅だと無いので不要

プロキシは環境に合わせて修正必要
以下の例でいくと、10.10.10.10:1010 ってとこ

環境確認

プロキシ確認
printenv http_proxy https_proxy

.bashrc への設定

\\wsl$\Ubuntu-20.04\home\shima.bashrc

を VS-Code などで編集して、以下を追加

.bashrc
# proxy
export http_proxy="10.10.10.10:1010"
export https_proxy=$http_proxy

\\wsl$\Ubuntu-20.04\etc\apt\apt.conf

同様に、以下も

apt.conf
Acquire::http::Proxy "http://10.10.10.10:1010";
Acquire::https::Proxy "http://10.10.10.10:1010";

参考

アップデート

update to get updating list, then execute updates
sudo apt-get update
sudo apt-get upgrade

初っ端はそこそこある。 pip3

コマンド一覧は以下より

python 準備

これも、そこそこHeavy

python
sudo apt install -y python3 python3-pip python-is-python3 python3-venv

pip3 の update は、上述のアップデートで
この段階でやったほうが楽だった・・のかな、とも?

pip version
pip3 -V
pip -V

使わない?と思われるが、pip のアップデートは以下

pip update
sudo pip3 install --upgrade pip

FastAPI の準備

インストール Fast API with uvicorn

pip3 install fastapi[all]

チュートリアルの実践で、Uvicorn 起動確認

fastapi_sample フォルダを用意して、main.py を VS-Code で開く
mkdir fastapi_sample_app
cd fastapi_sample_app
code main.py

で、以下のチュートリアルコードを貼り付けたあとで

python main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}
uvicorn 起動
uvicorn main:app --reload

ブラウザで、以下を確認して Hello Wordl が出ていれば OK
http://127.0.0.1:8000
image.png

ついでに、Open API ページの確認
http://127.0.0.1:8000/docs

あとがき

あとは、SQLAlchemy 、ODBC Driver を入れて、CRUD の Sample まで行ければ、かな

チュートリアルがほんと丁寧で感謝しかない

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?