Jonathan Newton

Deploying Python with Now 2.0

Deploying Python can be a bit confusing and without obvious tooling where do you start?

  • What platform do I use? Azure, AWS, Heroku or self host?

  • Do I need to buy a domain name? how will I share my applications?

  • Do I have to write a django application and then import my code?

If you are just writing proof of concepts, this can be off-putting and will end up just slowing you down.

Now

Now is a serverless deployment platform that removes hefty configuration files in favour of small functions.

Why serverless?

No servers to manage, configure, scale, operate and monitor = Less headaches.

How much does it cost?

It’s free.

Where does Now deploy my code?

Azure, AWS and GCP, but it depends where you are. Now will always try and match your region.

Installing now

Installing Now

Register Now

Example

In this example, we create a Python file index.py at the root of our application. This takes any get request to our function and returns a response of type text/html.

1
2
3
4
5
6
7
8
9
10
11
12
from http.server import BaseHTTPRequestHandler

class handler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
def do_GET(self):
self.do_HEAD()
message = "<h1>Jam</h1>"
self.wfile.write(message.encode())
return

We then define our config file now.json at the root of our application. We tell the Now builder to use the Python interpreter for all files ending in .py.

1
2
3
4
5
6
7
8
9
{
"version": 2,
"builds": [
{
"src": "*.py",
"use": "@now/python"
}
]
}

Using Now, we can also tell our builder to get any dependencies in the root directory simply create a requirements.txt file.

1
pandas=0.20.3

Now let Now… do the magic!

Now has built and deployed our application, we can access it here.

This is impressive! Given how little code we have written and how quickly we can get going, hopefully taking the confusion out of deploying Python applications.

References

Docs

Now Cli

SimpleRequestHandler