Interactive Salesforce Graphing in Python using plotly

\"plotly.PNG\"
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

\"plotlyreg\"

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.

\"apiplotly.png\"

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 plotly.tools as tls
    #plotly credentials under API settings
    tls.set_credentials_file(username=\'xxxxxx\',api_key=\'xxxxxxxxxxx\')
    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,Owner.name from Lead \')
    Statuses = [x[\'Status\']for x in lead_for_status[\'records\']]
    status_counts=Counter(Statuses)
    data=Data([Bar(x=status_counts.keys(),y=status_counts.values())])
    py.iplot(data,filename=\'lead_dist\')

    .

  7. Now save this file as plotlysfdc.py

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

python plotlysfdc.py

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.

\"plotlyooutput\"

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 »