Customer Banners (Ads) - SpiceUp. AX and SpotfireX Disclaimer



If you find this site useful and you want to support, buy me a coffee   to keep this site alive and without ads.

Export to Excel, PDF, Clipboard, etc (client and webplayer V6)

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:
  1. Extracts the data from the underlying data table from a given visualization to a temp file
  2. Writes the tmp file back to an html table includding a button that injects js and css libraries to provide the necessary files 
  3. 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 



13 comments:

Jason said...

In my html editor it says the element 'button' is not supported.

please advise. thanks

Jose Leviaguirre said...

what version are you using? This was done in Spotifre 5.5. HTML had limited capabilities back then. I'll try to find some time to make it compatible with 6+

Matt said...

Thank you for this and all your other work to improve capabilities! Is this a Table Viz feature only or is there a way to modify this to work with Cross Tables (or other Viz)

Jose Leviaguirre said...

You are Welcome Matt! This is an old post while working with spotfire < 6. So the way this works is that the script is extracting the data from the table viz and using such data to re-create an enhanced html table (with flash based export capabilites). So with some tweaks, you should be able to extract the data from a table plot.

Ant Reed said...

Hi Jose thanks for this resource it is extremely useful and well structured so that even a novice like myself can pick it up easily! I am using Spotfire V7 and trying to add the script above to help with exporting from the webplayer but I am having issues with adding the "scripting magic" from cdn, some of the links are no longer up and running I believe? could you give me a work around for this please??

Thanks in advance for your help and keep doing what your doing!!!

Jose Leviaguirre said...

Greetings Ant,

This is a very old post but I'll try to see if the export functionality from the SDK can work in latest 7.6 version. If not, I'll see if I can make this one work again somehow. This solution is based on flash, it's not the best solution, but it works.

Vidya Sagar Malla said...

This will be awesome if you are able to create the same in 7.6 Version. Also i believe the data in the HTML table are not dynamic to the filtering schemes right? If you could able to achieve this that would be awesome.

I really appreciate your thought process on this. Thanks a ton !!!

Unknown said...

Hi Jose,

This post would be perfect if I had the same version of Spotfire. But I'm not and since I'm a newbie I have difficulties to adapt this script.

Did you made it works on Spotfire 6 or 7 ?

Thanks a lot :)

Jose Leviaguirre said...

Hello Celia,

I experimented on V6. Will find some time to re-visti this in case the new Spotifre 7.6 is not able to export pdf from python as it is supposed to but unable to due to a bug.

Unknown said...

Hi,

This code works fine for me with small data. But if my data is long like 8494 KB then when I am reading data from temp file then my Spotfire is getting hanged due to that nothing works and at last I need to close the Spotfire screens.

Can you please suggest what is the issue and how to resolve?

thanks

Jose Leviaguirre said...

Hello Rakshita, What is exactly what you are trying to achieve? Export to excel or PDF? My guess is csv as this is a very common request. I suggest you not to use this solution for large datasets, but to store the data to a file in a location where you can get it via http. For example, you store your file on server at c:\inetpub\wwwroot\files\export.csv and generate a link for the user to download at http://yourdomain.com/files/export.csv

Ritu said...

Hi Jose,

I tried the functionality in 6.5 but it seems I am not able to get the export options to PDF, csv...
Could you please let me know if I am missing something.

Regards
Ritu

Jose Leviaguirre said...

Hello Ritu,

This is an old post and I no longer have 6.5 to troubleshoot. Besides, I believe TIBCO Spotfire 7.0 and under reached its end of life and it is no longer supported. I have not figured out an easy way to have the custom export functionality happen. So far, the best way is to train the end user how to export the data by using the out of the box functionality from both, the client and webplayer.