Jump to content
  • Spotfire Navigation Script using IronPython


    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 the next page, or 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 of current page navigation can be found at: http://spotfired.blogspot.nl/2014/08/toggle-page-navigation.html

    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

    this will create a multidimensional array "pages" with values as follows:

    [['Cover Page', 0], ['Home', 1], ['Page1', 2], ['Chapter1', 3], ['Chapter1 : Detail', 4], ['Metadata', 5]]

    Targeting a page by the string

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

    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'

    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 
        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.

        Document.ActivePageReference = Document.Pages[targetPageIndex]

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



    User Feedback

    Recommended Comments

    There are no comments to display.

  • Create New...