This script converts a given visualization into an html table (with limited funcionality) but most important, adds export to excel, csv, pdf, clipboard capabilities. You can simply hide the rendered html table and keep the desired functionality.
The way it works is as follows:
Notes: Do not edit the text are where the enhanced html table renders because TS will remove many non supported html tags
#Converts a tablePlot to an html table and adds export functionality. Works in webplayer and any browser that supports flash
# to POC, Add a data table and two text areas. Then define the two script parameters:
# visTA : target text area (a blank text area different from where this script will running as it will replace its contents)
# visDT : Data Table visualization
from Spotfire.Dxp.Application.Visuals import TablePlot, HtmlTextArea
ta = visTA.As[HtmlTextArea]()
from System.IO import Path, StreamWriter
from System.Text import StringBuilder
#Temp file for storing the cross table data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()
#Export TablePlot data to the temp file
tp = visDT.As[TablePlot]()
writer = StreamWriter(tempFilename)
tp.ExportText(writer)
#Build the table
sb = StringBuilder()
#Open the temp file for reading
f = open(tempFilename)
#add some scripting magic from CDN
html = '\
<button onclick=\'$("<link/>", { "rel": "stylesheet", "type": "text/css", "href": "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" }).appendTo("head");$("<link/>", { "rel": "stylesheet", "type": "text/css", "href": "https://datatables.net/release-datatables/extras/TableTools/media/css/TableTools.css" }).appendTo("head");$.getScript("https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js", function () { $.getScript("https://datatables.net/release-datatables/extras/TableTools/media/js/TableTools.min.js",function(){$("#myTable").dataTable({ "sDom": "T<clear>lfrtip", "oTableTools": { "sSwfPath": "https://datatables.net/release-datatables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"}})})})\'>download</button>\n\
'
#build the html table
html += " <TABLE id='myTable'>\n"
html += "<THEAD>"
html += " <TR><TH>"
html += " </TH><TH>".join(f.readline().split("\t")).strip()
html += " </TH></TR>"
html += "</THEAD>\n"
html += "<TBODY>\n"
for line in f:
html += "<TR><TD>"
html += "</TD><TD>".join(line.split("\t")).strip()
html += "</TD></TR>\n"
f.close()
html += "</TBODY>\n"
html += "</TABLE>\n"
ta.HtmlContent = html
The way it works is as follows:
- Extracts the data from the underlying data table from a given visualization to a temp file
- Writes the tmp file back to an html table includding a button that injects js and css libraries to provide the necessary files
- When the created button is clicked, the html table is converted into an enhanced html table with export functionality
Notes: Do not edit the text are where the enhanced html table renders because TS will remove many non supported html tags
#Converts a tablePlot to an html table and adds export functionality. Works in webplayer and any browser that supports flash
# to POC, Add a data table and two text areas. Then define the two script parameters:
# visTA : target text area (a blank text area different from where this script will running as it will replace its contents)
# visDT : Data Table visualization
from Spotfire.Dxp.Application.Visuals import TablePlot, HtmlTextArea
ta = visTA.As[HtmlTextArea]()
from System.IO import Path, StreamWriter
from System.Text import StringBuilder
#Temp file for storing the cross table data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()
#Export TablePlot data to the temp file
tp = visDT.As[TablePlot]()
writer = StreamWriter(tempFilename)
tp.ExportText(writer)
#Build the table
sb = StringBuilder()
#Open the temp file for reading
f = open(tempFilename)
#add some scripting magic from CDN
html = '\
<button onclick=\'$("<link/>", { "rel": "stylesheet", "type": "text/css", "href": "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" }).appendTo("head");$("<link/>", { "rel": "stylesheet", "type": "text/css", "href": "https://datatables.net/release-datatables/extras/TableTools/media/css/TableTools.css" }).appendTo("head");$.getScript("https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js", function () { $.getScript("https://datatables.net/release-datatables/extras/TableTools/media/js/TableTools.min.js",function(){$("#myTable").dataTable({ "sDom": "T<clear>lfrtip", "oTableTools": { "sSwfPath": "https://datatables.net/release-datatables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"}})})})\'>download</button>\n\
'
#build the html table
html += " <TABLE id='myTable'>\n"
html += "<THEAD>"
html += " <TR><TH>"
html += " </TH><TH>".join(f.readline().split("\t")).strip()
html += " </TH></TR>"
html += "</THEAD>\n"
html += "<TBODY>\n"
for line in f:
html += "<TR><TD>"
html += "</TD><TD>".join(line.split("\t")).strip()
html += "</TD></TR>\n"
f.close()
html += "</TBODY>\n"
html += "</TABLE>\n"
ta.HtmlContent = html
webplayer
TS 6.0 Client
ConversionConversion EmoticonEmoticon