Deploy Flask with ZEIT Now

I had a hard time trying to get Python (Flask) to work with NOW. The official document is also hard to see, so I will summarize it again.

Run Flask at a minimum

If you just run Flask, this works. The important point is the builds of now.json.

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" }]
}

Supports multiple routes

problem

Suppose you add a routing to index.py that handles / hello.

index.py


from flask import Flask
app = Flask(__name__)


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

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

It looks like it is 404

If you deploy in this state and access / hello, it will be 404.

image.png

Avoid by adding routes to now.json

To handle it properly, edit now.json and add routes. Now any request will be processed by wsgi root.

now.json


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

It seems that the request is being handled normally

image.png

Flask is supposed to handle all paths

Since routes have been added, any request that does not exist will be processed via Flask.

image.png

Customize 404 pages

You can customize it by looking at Custom Error Page in Flask Official Documentation

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

State

image.png

Recommended Posts

Deploy Flask with ZEIT Now
Deploy flask app with mod_wsgi (using pipenv)
IP restrictions with Flask
Hello world with flask
Programming with Python Flask
How to deploy a web app made with Flask to Heroku
Touch Flask + run with Heroku
Hello World with Flask + Hamlish
Unit test flask with pytest
API with Flask + uWSGI + Nginx
Deploy Django serverless with Lambda
SNS Flask (Ajax) made with Flask
Web application development with Flask
Python compilation with pyqt deploy
(Failure) Deploy a web app made with Flask on heroku
View flask coverage with pytest-cov
Web application with Python + Flask ② ③
File upload with Flask + jQuery
Web application with Python + Flask ④
[LINE login] Verify state with Flask
SNS Python basics made with Flask
[Memo] Links for developing with Flask
Creating a Flask server with Docker
Run the app with Flask + Heroku
Persist Flask API server with forever
Deploy a Django application with Docker
[Python] Use Basic/Digest authentication with Flask
Basic authentication and Digest authentication with Flask
Creating a simple app with flask
Build Flask environment with Dockerfile + docker-compose.yml
SNS made with Flask Flask (Blueprint, bcrypt)
Deploy Flask app on heroku (bitterly)
Run python3 Django1.9 with mod_wsgi (deploy)
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Post bulletin board creation with flask
Application development with Docker + Python + Flask
Image upload function with Vue.js + Flask