5
1

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 1 year has passed since last update.

Flaskの環境構築手順

Last updated at Posted at 2023-06-24

前回はFlaskについて説明しました。
今回はFlaskの環境構築についてまとめました

・前回の記事

環境構築手順

Macでの手順をまとめています

Pythonがインストールされているか確認します

$ python --version
Python 3.11.3

今回はvenvを使用して仮想環境上にFlaskの環境構築をしていきます
venvについては下記を参考にしてください

ディレクトリを作成します

mkdir flask-app

仮想環境を作成します

python -m venv flaskapp

仮想環境を有効化します

source flaskapp/bin/activate

Flaskをインストールします

pip install Flask

続いてアプリを作成していきます
サンプルファイルを作成します

touch sample.py

flaskのコードを書いていきます

sample.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World'

Flaskで作成したアプリを起動します

flask run -app sample

Chrome等のブラウザでlocalhostに接続すると画面上に「Hello World」が表示されます

また、ポート番号を指定することが可能です

flask run -app sample --port=8080

備考

仮想環境を無効化

deactivate

チーム開発

仮想環境を有効化してから実行

全ての依存関係のライブラリをインストールする方法

pip install -r requirements.txt

依存関係ファイルrequirements.txtを作成する方法

pip freeze > requirements.txt
5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?