Deploy a Flask App
Flask is a Python micro framework for building web applications.
This guide covers how to deploy a Flask app to Treasury in four ways:
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:
from flask import Flask:- This line imports the Flask class from the Flask framework, which is used to create and manage a web application.
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.
@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.
- The
def hello():- The
hellofunction 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.
- The
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
- Run the following command to install a production web server, gunicorn:
Next, run the following command to serve the app with gunicorn:
- Open your browser and go to
http://127.0.0.1:8000to 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.
- 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:
One-click deploy from a template
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
- Install the Treasury CLI:
- Install the CLI and authenticate it using your Treasury account.
- 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.
- Run the command below in your Flask app directory.
- 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.
- Use the command below to deploy your app:
- Verify the Deployment:
- Once the deployment completes, go to View logs to check if the server is running successfully.
- 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.

Deploy from a GitHub repo
To deploy a Flask app to Treasury directly from GitHub, follow the steps below:
-
Create a New Project on Treasury:
- Go to Treasury to create a new project.
-
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.
- Select Deploy from GitHub repo and choose your repository.
-
Deploy the App Service:
- Click Deploy on the Treasury project canvas to apply your changes.
-
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.
-
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
-
Create a
Dockerfilein the app's root directory. -
Add the content below to the
Dockerfile: -
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: