Treasury|Treasury Docs

Deploy a Flask App

deploymentflaskpythonbackend

Flask is a Python micro framework for building web applications.

This guide covers how to deploy a Flask app to Treasury in four ways:

  1. One-click deploy from a template.
  2. Using the CLI.
  3. From a GitHub repository.
  4. Using a Dockerfile.

Now, let's create a Flask app!

Create a Flask app

Note: If you already have a Flask app locally or on GitHub, you can skip this step and go straight to the Deploy Flask App to Treasury.

To create a new Flask app, ensure that you have Python and Flask installed on your machine.

Follow the steps blow to set up the project in a directory.

Create a project directory and cd into it.

Create a virtual environment

Activate the virtual environment

Note: For windows developers, run it as env\Scripts\activate in your terminal.

Install Flask

Now create a new file, helloworld.py in the flaskproject directory. Add the following content to it:

  1. from flask import Flask:
    • This line imports the Flask class from the Flask framework, which is used to create and manage a web application.
  2. app = Flask(__name__):
    • This line creates an instance of the Flask class and assigns it to the app variable.
    • The __name__ argument helps Flask identify the location of the application. It's useful for determining resource paths and error reporting.
  3. @app.route('/'):
    • The @app.route('/') decorator sets up a URL route for the app. When the root URL (/) is accessed, Flask will execute the function immediately below this decorator.
  4. def hello():
    • The hello function returns a plain text message, "Hello world, welcome to Treasury!", which is displayed in the browser when the root URL of the app is accessed.

Run the Flask app locally

To run the application, use the flask command.

Open your browser and go to http://127.0.0.1:5000 to see the app running with a local development server.

Prepare the Flask app for deployment

  1. Run the following command to install a production web server, gunicorn:

Next, run the following command to serve the app with gunicorn:

  1. Open your browser and go to http://127.0.0.1:8000 to see the app running with a production server.

Create a requirements.txt file to store the dependencies of the packages needed to run the app.

Note: It's only safe to run the command above in a virtual environment, else it will freeze all python packages installed on your system.

  1. Set the start command in your Treasury service settings to gunicorn main:app. See Start Command for more info.

With these changes, your Flask app is now ready to be deployed to Treasury!

Deploy Flask app to Treasury

Treasury offers multiple ways to deploy your Flask app, depending on your setup and preference. Choose any of the following methods:

  1. One-click deploy from a template.
  2. Using the CLI.
  3. From a GitHub repository.

One-click deploy from a template

Deploy on Treasury

It is highly recommended that you eject from the template after deployment to create a copy of the repo on your GitHub account.

Note: You can also choose from a variety of Flask app templates created by the community.

Deploy from the CLI

  1. Install the Treasury CLI:
  2. Initialize a Treasury Project:
    • Run the command below in your Flask app directory.
    • Follow the prompts to name your project.
    • After the project is created, click the provided link to view it in your browser.
  3. Deploy the Application:
    • Use the command below to deploy your app:
    • This command will scan, compress and upload your app's files to Treasury. You’ll see real-time deployment logs in your terminal.
  4. Verify the Deployment:
    • Once the deployment completes, go to View logs to check if the server is running successfully.
  5. Set Up a Public URL:
    • Navigate to the Networking section under the Settings tab of your new service.
    • Click Generate Domain to create a public URL for your app.
screenshot of the deployed Flask service

Deploy from a GitHub repo

To deploy a Flask app to Treasury directly from GitHub, follow the steps below:

  1. Create a New Project on Treasury:

    • Go to Treasury to create a new project.
  2. Deploy from GitHub:

    • Select Deploy from GitHub repo and choose your repository.
      • If your Treasury account isn’t linked to GitHub yet, you’ll be prompted to do so.
  3. Deploy the App Service:

    • Click Deploy on the Treasury project canvas to apply your changes.
  4. Verify the Deployment:

    • Once the deployment completes, go to View logs to check if the server is running successfully.

    Note: During the deployment process, Treasury will automatically detect that it's a Python app via Railpack.

  5. Set Up a Public URL:

    • Navigate to the Networking section under the Settings tab of your new service.
    • Click Generate Domain to create a public URL for your app.

Use a Dockerfile

  1. Create a Dockerfile in the app's root directory.

  2. Add the content below to the Dockerfile:

  3. Either deploy via the CLI or from GitHub.

Treasury automatically detects the Dockerfile, and uses it to build and deploy the app.

Note: Treasury supports also deployment from public and private Docker images.

Next steps

Explore these resources to learn how you can maximize your experience with Treasury: