Skip to main content

Creating an API with Flask in the Intelligence Module

To configure a Flask application in the Intelligence module, there are a few prerequisites. In this step-by-step guide, we will use a basic Flask application template.

Folder structure

.
├── app.py
└── main.orchest

Application code

from flask import Flask

app = Flask(__name__)

@app.route('/')
def welcome():
return "Welcome to my Flask application!"

if __name__ == '__main__':
app.run(host='0.0.0.0')

Important note: it is essential to set host='0.0.0.0' so your application is accessible from external IP addresses. With this configuration, the app accepts connections from any IP instead of only local connections, which would happen with 127.0.0.1.

Environment setup

  1. Configure the environment exactly as shown in the image below.
  2. Build the environment.

Creating the Flask service

  1. In the Intelligence module project, click Data Apps.

  1. Click Edit data apps.
  2. Click Add service.
  3. A dialog opens. Click Create custom service.
  4. A generic service is created. Click its row to edit the Data App.
  5. Configure the service with the following values:
    1. Image: select the environment you built.
    2. Command: bash
    3. Args: -c python3 /project-dir/app.py
    4. Project directory: /project-dir
    5. Data directory: /data
    6. Ports: enter the port exposed by your Flask application. The default port is 5000.
    7. Preserve Base Path: keep this option unchecked. If it is checked, because of the nginx configuration, the service is mapped to a path such as http://internal_ip:5000/pdp-service-flask-12278767-bd14-4731f258d482-fa66-4655_5000, but for this case it needs to be mapped to http://internal_ip:5000/.
    8. Exposed: this option must be enabled.
    9. Authentication Required: this option controls whether users must be authenticated in the Intelligence module before accessing the API.
Custom Flask Data App service configuration
  1. Save.
  2. Restart the session to activate the service.

Validation

When you open the URL exposed by the service, you should see the following result: