3
2

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.

ZEIT NowでFlaskをデプロイする

Posted at

NOWを使ってPython(Flask)をどうさせようとしたら苦戦した。公式ドキュメントも見づらいので改めてまとめる。

Flaskを最低限動かす

Flaskを動かすだけならこれで動作する。now.jsonの builds が重要なポイント。

index.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
    return "hello"
requirements.txt
flask==1.0.2
now.json
{
    "version": 2,
    "builds": [{ "src": "index.py", "use": "@now/python" }]
}

複数のルーティングに対応する

問題点

index.pyに /hello を処理するルーティングを追加したとする。

index.py
from flask import Flask
app = Flask(__name__)


@app.route("/")
def index():
    return "hello"

@app.route("/hello")
def world():
    return "world"

404になっている様子

この状態でデプロイして /hello にアクセスすると404になってしまう。

image.png

now.jsonにroutesを追加し回避する

適切に処理するためには、now.jsonを編集し、routesを追加する。これで、どんなリクエストでもwsgiのrootで処理されるようになる。

now.json
{
    "version": 2,
    "builds": [{ "src": "index.py", "use": "@now/python" }],
    "routes": [{ "src": "/.*", "dest": "/" }]
}

正常にリクエストをさばけている様子

image.png

すべてのパスをFlaskが処理するようになっている様子

routesを追加したため、存在しないパスへリクエストが来ると必ずFlask経由で処理される。

image.png

404ページをカスタマイズする

Flaskの公式ドキュメントのカスタムエラーページを見てカスタマイズできる

index.py
from flask import Flask, jsonify
app = Flask(__name__)

@app.route("/")
def index():
    return "hello"

@app.route("/hello")
def world():
    return "world"

@app.errorhandler(404)
def resource_not_found(e):
    return jsonify(error=str(e)), 404

様子

image.png

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?