pythonのライブラリflaskをインストールしていることが前提です。
app.pyというファイルを作成します。
以下の内容を記述します。
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
result = sum(range(1, 11))
return render_template("index.html", result=result)
if __name__ == "__main__":
app.run(debug=True)
次に、HTMLのベースとなるテンプレートファイルを作成します。
テンプレートは Jinja2のフォーマットの変数などを使ってHTMLを記載します。
templates/index.htmlに作成します。
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Flask 結果</title></head>
<body>
<h1>計算結果 (Flask)</h1>
<p>1〜10 の合計は <strong>{{ result }}</strong> です。</p>
</body>
</html>
アプリケーションとして起動する。
$ python app.py
しばらく待って、以下の表記が出てくると成功。
$ python app.py
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 643-342-407
ローカル環境で以下のURLで描画される。
http://127.0.0.1:5000/
