The free Starter Plan ended on September 23, 2024

Learn More
App Guides

Deploying a Python App

This guide will help you deploy your Python app or API to the Adaptable Cloud. In just a few clicks, Adaptable deploys all the cloud-based resources you need to run your Python app, including:

  • Building your app into Serverless Containers
  • Fully-managed Serverless Database (MongoDB, PostgreSQL, or MS SQL Server)
  • Autoscaling
  • Load Balancing
  • HTTPS (SSL/TLS)

»Step 1: Get your app ready

Before you get started deploying your existing app, you may want to review the Adaptable containerized app requirements.

»Listen on PORT

Make sure your Python app will listen for HTTP requests on the port specified by the PORT environment variable. The way Python apps specify this varies depending on the framework or library you use, but most apps either use the command line or code.

»If your app uses the command line

Many Python apps specify the listen port on their command line, using options to gunicorn, waitress-serve, or another WSGI server.

If your app uses the command line to specify what port to listen on, you can skip to the next step. You'll enter that command line information as the Start Command setting later, in Step 5.

»If your app uses code

If your app specifies the listen port using code, you may need to modify the code to use the PORT environment variable. Below is example code using HTTPServer from the Python standard library and showing how to use the PORT environment variable.

from http.server import HTTPServer
import os
# Get port number from the PORT environment varaible or 3000 if not specified
port = os.getenv('PORT', 3000)
server = HTTPServer(('0.0.0.0', port), MyServer)
server.serve_forever()
important

If you made any code changes for this step, don't forget to commit them to git and push them to GitHub.

»Step 2: Connect to GitHub

Start by clicking the button below:

Alternatively, you can go to your App Dashboard, then click the New App button and then Connect an Existing Repository.

If you haven't yet connected any of your GitHub repositories to Adaptable, click Connect GitHub Repository.

Choose which scope you'd like to connect Adaptable to. You can connect Adaptable to your personal GitHub scope or to an organization you're a member of.

And finally, click the Install & Authorize button.

»Step 3: Choose a GitHub repo to deploy

Next, choose the repo you'd like to deploy to the Adaptable Cloud.

If you don't see the repo you're looking for, click Add more GitHub repositories to authorize Adaptable on more of your repos or GitHub organizations.

List of GitHub repos

Then select the branch you'd like to deploy. Once connected, Adaptable sets up continuous deployment to build and deploy your repo each time you push to this branch.

»Step 4: Choose a Deploy Template

Since you're deploying a Python app, choose the Python App Template.

Choosing a deploy template

»Step 5: Set Start Command and review settings

The default settings for the Deploy Template are shown. For more information on each setting, click the help icon next to the setting or see the template documentation. To change a setting, click Change next to the setting you wish to change.

Reviewing template settings

Click the Change button next to Start Command and enter the command line to run your app. For many Python apps, this will be the command for the production WSGI server, such as gunicorn, that you're using with your app.

important

Make sure you use command line options to tell your app or WSGI server to listen on 0.0.0.0 and on the port specified by the PORT environment variable.

example

If you're using the Gunicorn WSGI server to start your app and your app's entrypoint function is named start_app in file myapp.py, then you'd enter:

gunicorn --bind=0.0.0.0:$PORT --threads=4 "myapp:start_app()"

Once you've set Start Command and are satisfied with all the settings, click Next.

»Step 6: Choose an app name

Choose a name for your app project on Adaptable. This name will be used to create a URL for your project like https://YOURAPPNAME.adaptable.app, so it must be unique among all other app names on Adaptable.

Enter your chosen app name and click Next.

»Step 7: Pre-flight checks

Adaptable will now perform some basic checks on your GitHub repo to ensure it's ready to deploy.

Most of the checks can be performed automatically. However, this screen asks you to manually confirm that your app listens for HTTP requests on the port specified by the environment variable PORT. (See Step 1 above.) Click the square checkbox to confirm.

For more information on any of the checks, see Pre-flight checks. If the checks do not pass or you have more questions on them, contact our support team for help.

When the checks have passed, click Deploy App to start deploying your new app.

Performing pre-flight checks

»Deployed!

Once deployment is complete, your app will be running at https://YOURAPPNAME.adaptable.app. You will be redirected to the app status page with a congratulations message if your app deployed successfully. Otherwise you'll get an error message and some help to resolve the error. In either case, you will have the option to take a tour to see how Adaptable works!

tip

Now that your app is set up on Adaptable, each time you push to the branch you selected on your GitHub repo, Adaptable will automatically deploy your code changes.

»Having trouble?

Look for the How to Fix This App tip box on your App Status page. It will help guide you toward logs and documentation appropriate for the error your app has.

If you need to make any code changes, you can push to your GitHub repo to update your app and Adaptable will re-deploy it.

If you're still having trouble deploying, contact our support team for help.

»Next Steps: Connect to the database

To use the fully-managed serverless database that Adaptable deploys with your app, you'll need to use the DATABASE_URL environment variable that Adaptable provides to your app.

Details of how Python apps connect to databases vary widely. But as an example, if you're using the SQLAlchemy ORM, you can pass the DATABASE_URL environment variable into create_engine similar to the code below:

from sqlalchemy import create_engine
import os
db_url = os.getenv('DATABASE_URL', 'postgresql://localhost:5432/mydatabase')
# DATABASE_URL uses postgres:// but SQLAlchemy only accepts postgresql://
db_url = db_url.replace('postgres://', 'postgresql://')
engine = create_engine(db_url)
...
with engine.connect() as connection:
result = connection.execute(...)
...

For more information on the database environment variables, see runtime environment variables.

Once you've made code changes, push them to your selected branch in GitHub and Adaptable will re-deploy your app automatically.

»Additional info

»Runtime Environment Variables

You can specify your own custom environment variables that will be available to your app while it is running. For more information on setting custom environment variables, see Customizing Your App Environment.

The template also automatically provides several pre-set environment variables to your app at runtime:

  • ADAPTABLE_TRUST_PROXY_DEPTH: When this variable is set, it indicates that the app is behind one or more reverse proxies. The value is an integer number of proxies that your app may trust in the X-Forwarded-For HTTP request header. If this variable is set, the X-Forwarded-Proto and X-Forwarded-Host HTTP request headers may also be trusted.

  • DATABASE_URL: This is the URL that your app should use to contact the database provisioned alongside your app. It will include the database protocol, port, and hostname, as well as the username, password, and specific database to connect to. It may also have additional query parameters containing database options. This URL can often be used directly by a library such as Sequelize. If you manually parse this URL, be aware that all special characters in each portion of the URL have been URL-encoded and should be decoded before use.
    Example: postgres://dbuser:secret@database-1.adaptable.io:1234/my-app-db?option=enabled

  • EXTERNAL_HOSTNAME: This is the externally visible host and domain name for your app.
    Example: my-app.adaptable.app

  • EXTERNAL_URL: This is the externally visible URL where your app users can contact your app. This will always begin with https://.
    Example: https://my-app.adaptable.app

  • PORT: The TCP port that your app must listen on to receive incoming network requests. Your app is required to listen on this port. See containerized app requirements for more information.

Apps that use a PostgreSQL database will also have the following environment variables set:

  • PGDATABASE: The name of the database to connect to.

  • PGHOST: The hostname of the database cluster.

  • PGPASSWORD: The password to use when connecting to the database. Note that this environment variable may contain special characters.

  • PGPORT: The TCP port number to connect to.

  • PGSSLMODE: Specifies whether to negotiate SSL/TLS with the database cluster. In Adaptable, this will always be verify-full. Adaptable-provisioned PostgreSQL databases require the use of SSL/TLS.

  • PGSSLROOTCERT: Path to a file containing SSL/TLS root certificates associated with the database cluster. In order for your PostgreSQL client to verify the database cluster's certificate, you must correctly configure your PostgreSQL client to verify using this root certificate.

  • PGUSER: The username to use when connecting to the database.

Apps that use a Microsoft SQL Server database will also have the following environment variables set:

  • MSSQL_DATABASE: The name of the database to connect to.

  • MSSQL_HOST: The hostname of the database cluster.

  • MSSQL_PASSWORD: The password to use when connecting to the database. Note that this environment variable may contain special characters.

  • MSSQL_TCP_PORT: The TCP port number to connect to.

  • MSSQL_USER: The username to use when connecting to the database.

note

Runtime environment variables are not available during your app's build process.

»Containerized app requirements

In order to run your app on the Adaptable Container Service, it must meet a few requirements. Most web-based apps shouldn't have trouble meeting these requirements.

  • Your app must listen for HTTP requests on 0.0.0.0 on the TCP port specified by the PORT environment variable. (Alternatively, your app can listen on a fixed port, but you will have to specify this PORT in your App's settings after the first failed deploy.)

  • Your app must be stateless. Although your app can write to local disk storage, that storage is a memory file system and will be lost when your app is scaled down or updated. Local disk storage is also not shared between multiple autoscaled instances of your app. This means your app cannot use local file-based databases, such as NeDB or SQLite.

  • Your app cannot perform background activities outside the scope of request handling. Your app's CPU allocation is set to zero when your app is not processing a network request.

  • You should not perform HTTPS/TLS in your app itself. Adaptable automatically provides HTTPS/TLS via our highly scalable load balancing tier.

  • When your app starts, it must listen for HTTP requests within 4 minutes of being started.