At ZEIT, we care deeply about making the cloud accessible for every developer, regardless of the programming language or framework they choose to use.
As part of our journey towards this goal, we are now announcing support for WSGI applications using the @now/python Builder.
This means that you can now adopt the serverless approach using the Python frameworks you already know and love (like Django or Flask), and benefit from blazing application performance, speedier builds and smoother deployments.
For example, you can now deploy serverless Flask apps:

A demo that showcases a Flask app deployed with Now using the @now/python Builder.

WSGI

The Web Server Gateway Interface (WSGI) is an implementation-agnostic interface for portable Python web application development.
Most Python web frameworks support WSGI.
By writing serverless WSGI apps, you can take advantage of all the benefits that Now provides:
  • Faster bootup time, as every route becomes a separate Lambda
  • Cheaper monthly spend, since you are only billed for the time in which requests are served.
  • Your code will be made accessible in a blazing fast manner from any location in the world, thanks to our built-in Smart CDN.
...and many more.

Getting Started

If you want to create a new serverless Python application, check out our guide.
Should you want to deploy an existing application, firstly ensure it has an app variable defined. This is how that would look for a Flask application:
from flask import Flask
app = Flask(__name__)

The app defined within index.py of a Flask project

Start by adding a now.json file to your project:
{
	"version": 2,
	"builds": [
		{ "src": "index.py", "use": "@now/python" }
	],
	"routes": [
		{ "src": "/(.*)", "dest": "index.py" }
	]
}

now.json helps you configure your deployment with your desired settings.

Once done, you can start developing locally with one command:
now dev
After that, deploy your app:
now

Adapting for Serverless

For the above example, we now want to build and deploy each page concurrently, so that each page corresponds to a different Lambda. Each individual Lambda can be scaled independently.
The first step to serverless is to handle routing outside of your application. This means the framework should use a wildcard, sometimes called a "catch all" route to serve the response for each page:
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):    
	return "You visited the %s page" % (path)

With the catch_all function, you can serve a response from any route

Using this pattern, you can create as many pages as desired, such as index.py, about.py or profile.py. The next step is to add routing to the now.json configuration file:
{
	"version": 2,
	"builds": [
		{ "src": "*.py", "use": "@now/python" }
	],
	"routes": [
		{ "src": "/(.*)", "dest": "/$1.py" }
	]
}

Routing can be specified within now.json with the routes property

  • We define the src property in builds as a Glob, so that each .py file results in a completely separate Lambda.
  • We define src property in routes as a Regular Expression, so that each path will route to the corresponding Lambda. For example, /about will be routed to the /about.py Lambda.

Static Files

Any files with static contents (such as .css and .js for the browser) do not need to be included in the Lambda. We can utilize the Smart CDN by including the @now/static Builder instead:
{
	"version": 2,
	"builds": [
		{ "src": "*.css", "use": "@now/static" },
		{ "src": "*.py", "use": "@now/python" }
	],
	"routes": [
		{ "src": "/(.*)", "dest": "/$1.py" }
	]
}

With the Smart CDN, all static files are served at maximum performance worldwide

All the files that end with .css get deployed to the CDN. For example, visiting /about.css will then retrieve the file from the Smart CDN, and not the Lambda.

Conclusion

Thanks to ZEIT Now, writing and deploying serverless Python applications is now just as easy as writing a regular monolith service.
This announcement opens up unlimited possibilities, and provides a path forward to enhance your development and application performance experience with existing Python frameworks. We cannot wait to find out what you build and deploy with it.
The Builder documentation can be found on our @now/python docs page. To try a fully working Flask example, take a look at Flaskex with PostgreSQL.
Should you have any questions, please do not hesitate to contact our support team.