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:
- Salesforce Username/Password/Security Token (signup for Developer org here).
- Python installed on your local computer with pip working (python windows / osx / linux – pip comes with Python 2.7x+).
- Plotly account
Now we have the formalities out the way lets get cracking.
Registering on plotly website:
- 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.
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.
- Open Terminal or Command Prompt on your computer.
- If your familiar with virtualenv then setup and activate it.
- Now your ready to install simple_salesforce, type:
pip install simple_salesforce
The same way, Please install pandas, numpy, requests libraries using pip
- If your python environment is setup correctly that should be it your now ready to build out your script.
- 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\')
- Now save this file as plotlysfdc.py
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.