npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gbozee/now-python-asgi

v1.0.27

Published

Python builder for ASGI applications on Zeit Now

Downloads

27

Readme

now-python-asgi

A Now builder for Python ASGI applications

Quickstart

If you have an existing WSGI app, getting this builder to work for you is a piece of 🍰!

1. Add a Now configuration

Add a now.json file to the root of your application:

{
    "version": 2,
    "name": "python-asgi-app",
    "builds": [{
        "src": "index.py",
        "use": "@gbozee/now-python-asgi",
        "config": { "maxLambdaSize": "15mb" }
    }]
}

NB: For ASGI2 support, Use @gbozee/[email protected]

This configuration is doing a few things in the "builds" part:

  1. "src": "index.py" This tells Now that there is one entrypoint to build for. index.py is a file we'll create shortly.
  2. "use": "@ardent-labs/now-python-wsgi" Tell Now to use this builder when deploying your application
  3. "config": { "maxLambdaSize": "15mb" } Bump up the maximum size of the built application to accommodate some larger python WSGI libraries (like Django or Flask). This may not be necessary for you.

2. Add a Now entrypoint

Add index.py to the root of your application. This entrypoint should make available an object named application that is an instance of your WSGI application. E.g.:

# For a sample Starlette app
from starlette.applications import Starlette
from starlette.responses import JSONResponse

application = Starlette()

@application.route('/')
async def homepage(request):
    return JSONResponse({'hello': 'world'})

# Replace `django_app` with the appropriate name to point towards your project's
# wsgi.py file

If the ASGI instance isn't named application you can set the wsgiApplicationName configuration option to match your application's name (see the configuration section below).

3. Deploy!

That's it, you're ready to go:

$ now
> Deploying python-asgi-app
...
> Success! Deployment ready [57s]

Requirements

Your project may optionally include a requirements.txt file to declare any dependencies.

Configuration options

runtime

Select the lambda runtime. Defaults to python3.7.

{
    "builds": [{
        "config": { "runtime": "python3.7" }
    }]
}

wsgiApplicationName

Select the WSGI application to run from your entrypoint. Defaults to application.

{
    "builds": [{
        "config": { "asgiApplicationName": "application" }
    }]
}

Additional considerations

Routing

You'll likely want all requests arriving at your deployment url to be routed to your application. You can do this by adding a route rewrite to the Now configuration:

{
    "version": 2,
    "name": "python-asgi-app",
    "builds": [{
        "src": "index.py",
        "use": "@gbozee/now-python-asgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/"
    }]
}

Avoiding the index.py file

If having an extra file in your project is troublesome or seems unecessary, it's also possible to configure Now to use your application directly, without passing it through index.py.

If your WSGI application lives in now_app/wsgi.py and is named application, then you can configure it as the entrypoint and adjust routes accordingly:

{
    "version": 2,
    "name": "python-asgi-app",
    "builds": [{
        "src": "now_app/asgi.py",
        "use": "@gbozee/now-python-asgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/now_app/asgi.py"
    }]
}

Lambda environment limitations

At the time of writing, Zeit Now runs on AWS Lambda. This has a number of implications on what libaries will be available to you, notably:

  • PostgreSQL, so psycopg2 won't work out of the box
  • MySQL, so MySQL adapters won't work out of the box either

Contributing

To-dos

  • [ ] Add tests for various types of requests

Attribution

This implementation draws upon work from: