Flask and psycopg2 : Pre-existing App Get Data

This article shows how to run a Python app with psycopg2 which uses PostgreSQL based Heroku Connect

../../_images/heroku-connect-flow-flask-psycopg2.png

Figure 1 : Heroku-Connect Data flow

Figure 1 show the how HerokuConnect Add-On interacts with Heroku Postgres and force.com behind the scenes Make sure you have Python installed. Also, install the Heroku Toolbelt

We will be using a basic Flask app available at flask_psycopg2_v1

This app has two rest endpoints

  • @app.route('/') which displays Hello world
  • @app.route('/contacts') which fetches list of contacts from Postgres table salesforce.contact

Clone the Source Code

$ git clone https://github.com/rajdeepd/flask-psycopg2-v1

Install Virtual Environment

Go to the root directory of the project cloned in the previous step and create a virtual environment env.

$ cd flask-psycopg2-v1
$ virtualenv venv
$ source venv/bin/activate

Install Dependencies

  1. Install the dependencies using pip install command

    $ pip install flask gunicorn psycopg2
    
  2. Run the app using the following command

    $ python app.py
    

    Your app should now be running on localhost:5000

You will see the default HelloWorld output as shown below.

../../_images/hello-world.png

If you try to access /contacts endpoint you will get a blank list.

../../_images/empty-contacts.png

Requirements File

Notice that the Requirements file already exists, this will be used by Heroku to setup the dynos

$ cat requirements.txt
Flask==0.10.1
gunicorn==19.3.0
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
psycopg2==2.6.1
Werkzeug==0.11
wheel==0.24.0

Procfile

There is already a Procfile which tells the Heroku what kind of Dyno is required and the source for the application.

web: gunicorn app:app --log-file -

Deploying to Heroku

$ heroku create
$ git push heroku master
$ heroku open

Add PostgreSQL Add-On

Add Postgress Add-On as shown below

$ heroku addons:create heroku-postgresql:hobby-dev

Add Heroku Connect Add-On

Configure Heroku Connect Add-On. Command below configures Herok-Connect Add-On to the application.

$ heroku addons:create herokuconnect

Open the addon in Heroku Dashboard

$  heroku addons:open herokuconnect

Configure Herok Connect Add-On

  1. Setup Connection
../../_images/setup-connection.png
  1. Enter Schema Name : This is the schema name underwhich database will be created.
../../_images/enter-schemaname.png
  1. Trigger OAuth
../../_images/trigger-oauth.png
  1. Enter Salesforce.com developer account credentials
../../_images/oauth.png
  1. Create Mappings
../../_images/create-mappings.png
  1. Create Mappings Contacts : Choose the fields in Salesforce Schema which need to be mapped to Postgres Database in the application.
../../_images/create-mapping-contacts.png
  1. Explore Contacts in the Dashboard
../../_images/contacts-explorer.png

Open the App again in Heroku

$ heroku open

Show Contacts

Browse to URL http://{your-app-name}.herokuapp.com/contacts to see the list of contact names.

../../_images/show-contacts.png

Show Contacts locally

Configure the DATABASE_URL in the local environment

$ heroku config

=== rocky-hamlet-29356 Config Vars
DATABASE_URL:      postgres://<user_name>:<password>@<ipaddress>.compute-1.amazonaws.com:5432/<database_name>
HEROKUCONNECT_URL: DATABASE_URL:salesforce

Export DATABASE_URL

$ export DATABASE_URL=postgres://<user_name>:<password>@<ipaddress>.compute-1.amazonaws.com:5432/<database_name>

Open the following URL http://localhost:5000/contacts you should be able see the contacts.

Summary

In this tutorial we learnt how to configure a Pre-existing Python Flask Application to work with Heroku Connect. We used Psycopg2 driver for talking to the PostgreSQL database deployed on Heroku.