Giter VIP home page Giter VIP logo

now-python-wsgi's Introduction

now-python-wsgi

A Now builder for Python WSGI applications

NPM version Build Status License

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-wsgi-app",
    "builds": [{
        "src": "index.py",
        "use": "@ardnt/now-python-wsgi",
        "config": { "maxLambdaSize": "15mb" }
    }]
}

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": "@ardnt/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 Dango app
from django_app.wsgi import application
# Replace `django_app` with the appropriate name to point towards your project's
# wsgi.py file

Look at your framework documentation for help getting access to the WSGI application.

If the WSGI 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-wsgi-app
...
> Success! Deployment ready [57s]

Requirements

Your project may optionally include a requirements.txt file to declare any dependencies, e.g.:

# requirements.txt
Django >=2.2,<2.3

Be aware that the builder will install Werkzeug as a requirement of the handler. This can cause issues if your project requires a different version of Werkzeug than the handler.

Configuration options

runtime

Select the lambda runtime. Defaults to python3.6.

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

wsgiApplicationName

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

{
    "builds": [{
        "config": { "wsgiApplicationName": "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-wsgi-app",
    "builds": [{
        "src": "index.py",
        "use": "@ardnt/now-python-wsgi"
    }],
    "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-wsgi-app",
    "builds": [{
        "src": "now_app/wsgi.py",
        "use": "@ardnt/now-python-wsgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/now_app/wsgi.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
  • Sqlite, so the built-in Sqlite adapter won't be available

Contributing

To-dos

  • Add tests for various types of requests

Attribution

This implementation draws upon work from:

now-python-wsgi's People

Contributors

dependabot[bot] avatar jayhale avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

now-python-wsgi's Issues

Django migrations

First, awesome work guy! Really looking forward to use this library.

Second, do you know of a proper way to run Django migrations after the build?

Best regards
Severin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.