Interactive Salesforce Graphing in Python using plotly

In this post we’ll explore how you can implement a interactive salesforce graphing in python using plotly.

We can\’t display a chart directly in python,however we can achieve with the help of  pandas and plotly libraries.

To achieve this,we need to register on plotly because chart is going to store and display in plotly website and from there we can download into our local system or we can share it with business users.

What you’ll need:

  1. Salesforce Username/Password/Security Token (signup for Developer org here).
  2. Python installed on your local computer with pip working (python windows / osx / linux – pip comes with Python 2.7x+).
  3. Plotly account

Now we have the formalities out the way lets get cracking.

Registering on plotly website:

  1. Please register on plotly website


2. Click on Settings and you see user name and api key under API keys section.

please not down these two parameters to authenticate plotly from python.


Python Script

Now for the fun bit first up we need to prepare our local environment so that we can be sure before copying onto our remote environment that it’s all working.

  1. Open Terminal or Command Prompt on your computer.
  2. If your familiar with virtualenv then setup and activate it.
  3. Now your ready to install simple_salesforce, type:
    pip install simple_salesforce
  4. The same way, Please install pandas, numpy, requests libraries using pip
  5. If your python environment is setup correctly that should be it your now ready to build out your script.
  6. As with other guides I’ve tried to comment the code in english so you can copy/paste and follow what each line is doing. Using an editor like Sublime create a file or use pycharm editor as per your favored (viz. programmer) choice.
    #import pandas and numpy
    import pandas as pd
    import numpy as np
    from collections import Counter
    import requests
    import plotly.plotly as py
    import as tls
    #plotly credentials under API settings
    from plotly.graph_objs import *
    #import salesforce Rest Client
    from simple_salesforce import Salesforce
    session = requests.Session()
    # manipulate the session instance (optional)
    sf = Salesforce(
       username=\'xxxxxx\', password=\'xxxxxx\', organizationId=\'xxxxx\',session=session)
    lead_for_status = sf.query(\'select id,Status, from Lead \')
    Statuses = [x[\'Status\']for x in lead_for_status[\'records\']]


  7. Now save this file as

So we have the file, lets now doing a quick test, from your terminal.


That’s it now just so long as you have followed the instruction here, go to plotly account and click on home page, you will see below output files generated in plotly, one with lead_dist dashboard and another is lead_dist grid file.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Please check your mail for confirmation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »