Knowledge Base Article

Embed URL Generation Made Easy With Python Script

Creating an embed URL is a multistep process that can easily be botched along the way. Finding where the mistake was made can be tricky, and the generation of multiple embed URLs can be a painful process to repeat. With a simple python script we can create a program that generates the embed URL for us by just entering the json blob. 
import hashlib
import hmac
import urllib
import json
import ast

api_key = 'abc-123456'

json_input = raw_input('input your json encoded dashboard:')
to_dict = ast.literal_eval(json_input)
json_data = json.dumps(to_dict)


encoded_json = (urllib.urlencode({"data": json_data})).split("=")[1]

url = "/api/embedded_dashboard?data=" + encoded_json
sig = hmac.new(api_key, url, hashlib.sha256).hexdigest()

print "https://www.periscopedata.com" + url + "&signature=" + sig
The above example is the fastest way to generate the direct link to the embedded dashboard. However, in some cases, the resulting URL will be too long for the web browser to parse. In those situations, you'll need to let the Sisense for Cloud Data Teams API create the url for you. Consider the following example:
import json
import requests

payload = {"dashboard":MyDashboardID}
site_name = 'my-site-name'
api_key = 'my-site-api-key'

url = 'https://app.periscopedata.com/api/v1/shared_dashboard/create'
headers = {'HTTP-X-PARTNER-AUTH': site_name + ":" + api_key}
data = json.dumps(payload)
response = requests.post(url, headers=headers, data=data)

try:
    print(json.loads(response.text)['url'])
    print(json.loads(response.text)['url']+'?border=false&embed=v2')
except:
    print('Couldn''t generate url: ' + response.text)
Declare three variables payload, site_name, and api_key and copy paste the rest. Payload should be a valid json object as defined here. You can validate yours at a site like this one: https://jsonlint.com/ . Site name and dashboard id can be found in your url bar like in the image below:
And lastly, admins can find the api key via Billing & Authentication in the Settings menu.
Now that we've created so many embedded dashboards, we need some way to manage them! Below are two scripts that help with that. We have two functions, one to list all embeds for a dashboard and one to delete an embedded dashboard. 

List all Embedded Dashboards for a Given Dashboard
import json
import requests

site_name = 'my-site-name'
api_key = 'my-site-api-key'
dashboard_id = my-dashboard-id

url = 'https://app.periscopedata.com/api/v1/shared_dashboard/list'

headers = {'HTTP-X-PARTNER-AUTH': site_name + ":" + api_key}
data = json.dumps({"dashboard":dashboard_id})
response = requests.post(url, headers=headers, data=data)

try:
    [print(i) for i in json.loads(response.text)]
except:
    print('Couldn''t list dashboards: ' + response.text)
This list script will provide the tokens (needed for the deletion script) as well as the urls of the embedded dashboards.

Delete a specific embedded dashboard
import json
import requests

site_name = 'my-site-name'
api_key = 'my-site-api-key'
token = 'my-dashboard-token'

url = 'https://app.periscopedata.com/api/v1/shared_dashboard/delete'

headers = {'HTTP-X-PARTNER-AUTH': site_name + ":" + api_key}
data = json.dumps({"token":token})
response = requests.post(url, headers=headers, data=data)

try:
    print(json.loads(response.text))
except:
    print('Couldn''t delete: ' + response.text)
The response for the delete API is an object that tells how many dashboards were deleted. If none were successfully deleted, the value will be 0. If it deletes 1, it will be 1!

For Python 3:
import hashlib
import hmac
import urllib
import json
import ast
import base64

api_key = b'insert key here'

json_input = input()
to_dict = ast.literal_eval(json_input)
json_data = json.dumps(to_dict)

encoded_json = (urllib.parse.urlencode(({"data": json_data})).split("=")[1])

url = "/api/embedded_dashboard?data=" + encoded_json
sig = hmac.new(api_key, url.encode(), hashlib.sha256).hexdigest()

print("https://www.periscopedata.com" + url + "&signature=" + sig)
Updated 03-02-2023
No CommentsBe the first to comment