Knowledge Base Article
The introduction appears to be hidden by JS and CSS in some browsers, below is the article introduction:
Sisense includes the capability to export all dashboard metadata in JSON format, both via the UI and via API. These include all metadata components of the dashboard and widgets, including widget and dashboard scripts. A common use case is to extract dashboard and widget scripts from one or more dashboard files, properly format JavaScript by converting escaped JSON strings into readable JavaScript, and remove default comment headers that are often present. This article provides a comprehensive Python script that automates extracting these scripts into separate files, formats them clearly, removes common default comments, organizes scripts by dashboard in separate folders, and adds a comment in each file indicating relevant details like the last viewed timestamp, widget type, and the URL to access the dashboard or widget.
The code also appears to have had the line breaks removed:
#!/usr/bin/env python3
"""
sisense_script_extractor.py
This script scans the current directory for all files ending with “.dash” (Sisense dashboard export file),
extracts the script code stored in the dashboard-level `script` field and in each widget’s `script` field,
beautifies (formats) the code, removes known default template comment blocks, and saves the cleaned and formatted scripts to a structured
“results/{dashboard_title}_{dashboard_oid}” folder. At the end of each output file, it appends a JS comment
indicating the dashboard’s lastOpened timestamp, widget type, and the relevant Sisense URL path.
Usage:
python sisense_script_extractor.py
Dependencies:
- jsbeautifier (install via `pip install jsbeautifier`)
"""
import json
import os
import re
from pathlib import Path
import jsbeautifier as beautifier
# Configuration constants
RESULTS_DIR = Path("results")
DEFAULT_TEMPLATE_DASHBOARD_SCRIPT = """/*
Welcome to your Dashboard's Script.
To learn how you can access the Widget and Dashboard objects, see the online documentation at https://sisense.dev/guides/js/extensions
*/"""
WIDGET_TEMPLATE_REGEX = r"/\*.*?see the online documentation at.*?\*/"
# -----------------------------------------------------------------------------
def beautify_js_code(js_code: str) -> str:
"""Beautify JavaScript code using jsbeautifier with a 4-space indent."""
opts = beautifier.default_options()
opts.indent_size = 4
return beautifier.beautify(js_code, opts)
# -----------------------------------------------------------------------------
def write_script_file(js_code: str, output_path: str, footer_comment: str) -> None:
"""Format JavaScript script, append footer comment, and write to specified file."""
cleaned_code = beautify_js_code(js_code)
full_content = cleaned_code + "\n\n" + footer_comment
with open(output_path, "w", encoding="utf-8") as file:
file.write(full_content)
print(f"Wrote script to {output_path}")
# -----------------------------------------------------------------------------
def process_dashboard_file(dash_file_path: Path) -> None:
"""
Load, extract, format, and save scripts from a Sisense dashboard file (.dash).
"""
print(f"Processing dashboard file: {dash_file_path.name}")
dashboard = json.loads(dash_file_path.read_text(encoding="utf-8"))
# Create output directory: results/{title}_{oid}
title_safe = dashboard.get("title", "dashboard").strip().replace(" ", "_")
oid = dashboard.get("oid", "unknown")
dashboard_output_dir = RESULTS_DIR / f"{title_safe}_{oid}"
dashboard_output_dir.mkdir(parents=True, exist_ok=True)
last_opened = dashboard.get("lastOpened", "unknown")
# --- Dashboard-level script extraction and cleanup ---
dash_script = dashboard.get("script")
if isinstance(dash_script, str) and dash_script.strip():
cleaned_dash = dash_script.replace(DEFAULT_TEMPLATE_DASHBOARD_SCRIPT, "")
footer = (
f"// Dashboard last opened on {last_opened}\n"
f"// To view dashboard URL Path is /app/main/dashboards/{oid}"
)
out_path = dashboard_output_dir / "dashboard_script_1.js"
write_script_file(cleaned_dash, str(out_path), footer)
# --- Widget-level script extraction and cleanup ---
widgets = dashboard.get("widgets", [])
if isinstance(widgets, list):
widgets_dir = dashboard_output_dir / "widgets"
widgets_dir.mkdir(exist_ok=True)
for widget in widgets:
widget_script = widget.get("script")
if isinstance(widget_script, str) and widget_script.strip():
widget_clean = re.sub(WIDGET_TEMPLATE_REGEX, "", widget_script, flags=re.DOTALL)
widget_oid = widget.get("oid", "unknown")
widget_type = widget.get("type", "unknown")
footer = (
f"// Dashboard last opened on {last_opened}\n"
f"// Script is for widget type of {widget_type}\n"
f"// To view widget URL Path is /app/main/dashboards/{oid}/widgets/{widget_oid}"
)
out_path = widgets_dir / f"{widget_oid}_WidgetScript.js"
write_script_file(widget_clean, str(out_path), footer)
# -----------------------------------------------------------------------------
def main():
"""Entry point: process all .dash files in the current directory."""
dash_files = list(Path(".").glob("*.dash"))
if not dash_files:
print("No .dash files found in the current folder.")
return
for dash_path in dash_files:
process_dashboard_file(dash_path)
if __name__ == "__main__":
main()