Spotfire Navigation Script leveraging Python

in spotfire •  7 years ago  (edited)

f434627b-a017-42cd-aded-8fba881b9bd4.jpg
Spotfire has built in ways for navigating, however when you want to insert your own browsing methods in a text area using images, buttons or texts for next page, previous page or even want to search for a parent page using Python you are hopelessly lost. Until now.
The way to navigate currently is to use the following code: Document.ActivePageReference = Document.Pages[0] telling Spotfire the active page should be the first [0] page of the collection of pages (in Document.Pages].
Examples on current page navigation can be found on spotfired blogspot

Page index for navigating

Navigating to a page is not done by Title but by index number which we do not have or only based on logic by counting the tab positions -1.
Therefore we need to create our own page index.

Creating a page index

pageId = 0
pages = []
for p in Document.Pages:
    arr = [p.Title, pageId]
    pageId = pageId +1 
    pages.append(arr)

this will create a multidimensional array "pages" with values like [['Cover Page', 0], ['Home', 1], ['Page1', 2], ['Chapter1', 3], ['Chapter1 : Detail', 4], ['Metadata', 5]]

Targeting a page by string

To find the page index number using a string comparison use:

targetPageIndex=0
for page in pages:
    if page[0] == "Home":  #looking for the first entry in the sub-array ['Home', 1]
        targetPageIndex = page[1]
        #print targetPageIndex

targetPageIndex will be 1

Targeting Next Page

for page in pages:
    print page
    if page[0] == Document.ActivePageReference.Title:
        targetPageIndex = page[1]+1 #current page index + 1 
        #print targetPageIndex

Targeting Previous Page

for page in pages:
    print page
    if page[0] == Document.ActivePageReference.Title:
        targetPageIndex = page[1]-1 #current page index - 1 
        #print targetPageIndex

Targeting the 'Parent' Page from 'Child' Page

In the pages array are two pages that start with the same name "Chapter1". When I am on page "Chapter1 : Detail and want to go back to the main Chapter1 page use the following script:

thisPage = Document.ActivePageReference.Title
matchingPattern = " : "
if not thisPage.find(matchingPattern) == -1:
    chapterPage = thisPage[:thisPage.find(matchingPattern)]
    #print chapterPage 
else: 
    chapterPage = Document.ActivePageReference.Title

for page in pages:
    if page[0] == chapterPage:
        targetPageIndex = page[1]
        #print targetPageIndex

Let the script navigate to the target page

Now that we have the targetPageIndex we can go to the page by using the simple page navigation mentioned earlier, however the index is specified by the variable rather than being user-determined.

try: 
    Document.ActivePageReference = Document.Pages[targetPageIndex]
except:
    pass

The reason I use a try/except is because Next Page and Previous Page navigation numbers can be out of the bounds of the Document.Pages index generating an error.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @gtenkate! You have received a personal award!

2 Years on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:

SteemitBoard - Witness Update
SteemFest³ - SteemitBoard support the Travel Reimbursement Fund.

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @gtenkate! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 3 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!