Wednesday, October 4, 2017

Spotfire - Replace and refresh data and onclick button loading event (HTML and JS)

ironPython:

import System
import Spotfire.Dxp.Application
from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Data.Import import DataTableDataSource
from Spotfire.Dxp.Data import CalculatedColumn
from System.IO import FileStream, FileMode, File, MemoryStream, SeekOrigin, StreamWriter, StreamReader
import System.String
from Spotfire.Dxp.Data.Import import TextDataReaderSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource
import clr
clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WinForms
from Spotfire.Dxp.Framework.ApplicationModel import NotificationService
from System.Windows.Forms import OpenFileDialog
from System.Collections.Generic import List, Dictionary
from Spotfire.Dxp.Application.Visuals import HtmlTextArea
from Spotfire.Dxp.Framework.ApplicationModel import *

#get data source location
file_loc = OpenFileDialog()
file_loc.InitialDirectory
file_loc.ShowDialog()

try: #validate the source

ds = Document.Data.CreateFileDataSource(file_loc.FileName)

     #since source_tbl is linked to source, need to replace it
source_tbl = Document.Data.Tables["well_attributes"].ReplaceData(ds)

except ValueError:
WinForms.MessageBox.Show("No file selected! Please select the file to refresh.")
        #refresh text area
vis.As[HtmlTextArea]().HtmlContent += " " # Force re-render
raise #Rethrow the original exception, so I get a stack trace to it.

 except IOError:
WinForms.MessageBox.Show("Please select the right file type.")
vis.As[HtmlTextArea]().HtmlContent += " "
raise

#notification service
notify = Application.GetService[NotificationService]();

# Tables(s) to refresh - change/add more if required
Tbls = List[DataTable]()
Tbls.Add(Document.Data.Tables['myTableName'])


# After tables are refreshed, update html
def afterLoad(exception, Document=Document, notify=notify):
if not exception:

# Hide the loading div
newHtml = vis.As[HtmlTextArea]().HtmlContent
oldDiv = '<div id="loading" style="display:inline">'
newDiv = '<div id="loading" style="display:none">'
newHtml = newHtml.Replace(oldDiv,newDiv)

# Apply change
vis.As[HtmlTextArea]().HtmlContent = newHtml;
vis.As[HtmlTextArea]().HtmlContent += " " # Force re-render

else:
notify.AddErrorNotification("Error refreshing table(s)","Error details",str(exception))

# Refresh table(s)
Document.Data.Tables.RefreshAsync(Tbls, afterLoad)

#message box after successful load and refresh data
WinForms.MessageBox.Show("Data has been successfully refreshed! ")

HTML:

<p style="text-indent: 4em;"> <font size="2"><strong> Please click to select well_attributes file (.csv file) to refresh:</strong></font>
<div id="clicker" style="visibility: visible;">
<SpotfireControl id="67e98bb593da44afaf6cfda44a8030e2" />
<font color="red" size="2"><strong>
<div id="loading"
style="display: none;">Refreshing...Please wait. DO NOT load file again!
</div></strong></font>
</div>

Javascript:
$('#67e98bb593da44afaf6cfda44a8030e2').click(function() {
   
// Show the loading prompt
$('#loading').css('display', 'inline');
// Click the python button 
$('#clickerpy input').click()
// disable the refresh button 
document.getElementById("67e98bb593da44afaf6cfda44a8030e2").disabled = true
   
});


5 comments:

  1. what this code does is to pop up file selection window and load csv file. then replace the source file and refresh corresponding data table. on click the button, then trigger javascript to show loading progress and once done, pop up successful load message.

    ReplyDelete
  2. I got this error. could you please tell me why:
    "NameError: name 'vis' is not defined

    Microsoft.Scripting.Runtime.UnboundNameException: name 'vis' is not defined
    at IronPython.Runtime.PythonContext.MissingName(SymbolId name)
    at Microsoft.Scripting.Runtime.LanguageContext.LookupName(CodeContext context, SymbolId name)
    at Microsoft.Scripting.Runtime.RuntimeHelpers.LookupGlobalName(CodeContext context, SymbolId name)"

    ReplyDelete
    Replies
    1. you have to add vis in your parameter. it should be the type "visulization" and value should be the page you put your button.

      Delete
    2. It works like a charm. Thank you

      Delete
  3. This comment has been removed by the author.

    ReplyDelete