19
23

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 5 years have passed since last update.

Python+Flask+MySQL on WindowsではじめるWebアプリ開発 #Step1

Last updated at Posted at 2018-09-30

はじめに

  • 初心者が簡単なWebアプリを開発していきます。
  • 手順をStep-by-Step方式で記載していきます。

開発環境

  • Windows10 Home
  • Python3.7.0 (pip 18.0)
  • Flask 1.0.2
  • Jinja2 2.10

仮想環境の構築

  • venvにて仮想環境を構築する。
C:\pystudy\lesson-flask>python -m venv step1

仮想環境の有効化

C:\pystudy\lesson-flask>step1\Scripts\activate

pipのアップグレード

(step1) C:\pystudy\lesson-flask>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0

Flaskをインストール

(step1) C:\pystudy\lesson-flask>pip install flask
Collecting flask
  Using cached https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl
  :
  :
 Installing collected packages: Werkzeug, itsdangerous, MarkupSafe, Jinja2, click, flask
  Running setup.py install for itsdangerous ... done
  Running setup.py install for MarkupSafe ... done
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-0.24

インストールパッケージの確認

(step1) C:\pystudy\lesson-flask>pip freeze
Click==7.0
Flask==1.0.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1

Hello Flask

アプリケーション

hello.py
from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(debug=True)

環境変数の設定

(step1) C:\pystudy\lesson-flask>SET FLASK_APP=hello.py

サーバの起動

flask実行
(step1) C:\pystudy\lesson-flask>flask run --debugger --reload
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 430-658-122
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Webブラウザにてアクセス

flask-fig0.png

サーバーの停止

Ctrl+c

仮想環境の無効化

(step1) C:\pystudy\lesson-flask>step1\Scripts\deactivate

今回のまとめ

  • flask runで起動するには、環境変数FLASK_APPにファイル名をセットする。
  • または、ファイル名をapp.pyにする。
Error: Could not locate a Flask application.
You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
  • 開発サーバは本番環境では使わない。
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.

Step2で実施すること

  • テンプレートを使ってみます。

参考リンク

19
23
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
19
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?