top of page

Python Script to export a Sisense widget to CSV

Here is the python script to export your Sisense widget to csv.


Update the following variables in the script:

localpath: Folder to which CSV file need to download

server Name: Name of server where Sisense is installed (first part of your dashboard URL)

username: Username(email) that you are using to login to Sisense.

password: Password to login your account

dashboardid: Id of dashboard where widget can be found

widgetid: Id of widget from where CSV need to downlead



import json import requests import urllib.parse localpath = 'C:\Sisense\CSV_Files\\' server_name = 'https://xxxxxx.xxxxxxxxxx.com' username = 'enter sisense login name (email)' password = 'enter your password here' dashboardid = 'dashboard id' widgetid = 'widget id' login_url = server_name + '/api/v1/authentication/login' dash_export_base_url = server_name + '/api/v1/dashboards/export?dashboardIds=' login_data = { 'username' : username, 'password' : password } # Generating token login_res = requests.post(url=login_url, data=login_data).json() access_token = login_res.get('access_token') if len(access_token) > 0: print('login successful') api_header = {'Authorization': 'Bearer ' + access_token, 'content-type': "multipart/form-data; boundary={}"} # Get widget details get_widget_url = server_name + '/api/v1/dashboards/' + dashboardid + '/widgets/' + widgetid get_widget_res = requests.get(url=get_widget_url, headers=api_header).json() widget_json = {} widget_metadata = [] widget_json["datasource"] = get_widget_res.get("datasource") panels = get_widget_res.get("metadata").get("panels") #Add metadata from widget for panel in panels: items = panel.get("items") for item in items: if(not item.get("disabled") == True): if (panel.get("name") == "filters"): widget_metadata.append({"jaql" : item.get("jaql"), "panel":"scope"}) else: widget_metadata.append({"jaql" : item.get("jaql")}) dash_filter_ignore = get_widget_res.get("metadata").get("ignore") ignore_list = dash_filter_ignore.get("dimensions") #Add dashboard filters if(dash_filter_ignore.get("all") == False): dash_file = requests.get(url=dash_export_base_url + dashboardid, headers=api_header, allow_redirects=True).json() dash_filters = dash_file[0].get("filters") for dash_filter in dash_filters: if(not dash_filter.get("disabled") == True and not dash_filter.get("jaql").get("dim") in ignore_list): widget_metadata.append({"jaql" : dash_filter.get("jaql"), "panel":"scope"}) widget_json["metadata"] = widget_metadata widget_json["format"] = "csv" widget_json["widgetType"] = "pivot2" widget_json["by"] = "export" widget_json["isMaskedResponse"] = "true" widget_json["filename"] = "widget.csv" widget_json["download"] = "true" widget_json["widget"] = widgetid widget_json["dashboard"] = dashboardid widget_json["culture"] = "en-US" # Calling API to download CSV datasource = urllib.parse.quote(get_widget_res.get('datasource').get('title')) download_csv_url = server_name + '/api/datasources/' + datasource + '/jaql/csv?data=' + urllib.parse.quote(json.dumps(widget_json)) donwloaded_csv = requests.post(url=download_csv_url, headers=api_header, allow_redirects=True) open(localpath + widgetid + '.csv', 'wb').write(donwloaded_csv.content) print('CSV downloaded') requests.get(server_name + '/api/v1/authentication/logout')


443 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page