The response has been extremely positive. Many users identify with the struggle to learn IronPython (and other languages) for Spotfire. One individual reached out to say that he needed more information on where/how to apply the code. I realized I’ve written that post for TERR, but I haven’t for IronPython. So, this post will explain implementing IronPython code. Let’s get to it.
Where do you put the code?
So, you found an IronPython code snippet to apply in your own DXP, but you don’t know where it goes. Perhaps, you found something on Brock’s website.
Now, unlike TERR scripts, IronPython scripts don’t run automatically. They must be triggered, usually by a button or a property control, which live in text areas. Therefore, IronPython scripts start in text areas. So, add a text area and follow the instructions below.
First, add text area.
Right-click and select Edit HTML. You can also select Edit Text Area.
Then, click the insert action control button in the toolbar.
You’ll then need to decide whether you want to click on a Button, Link or Image. Buttons are most common. Make a selection in the Control type drop down.
Give it a name in the Display text input box.
Now, click on the Script button on the left hand side of the dialog.
This opens a new dialog where you want to click New.
Name the script.
Copy and paste the script in the script window. Make any modifications as needed.
Add any required parameters.
Finally, click Run Script to make sure it works.
If needed, you may edit the script from the text area going thru similar steps. Or, IronPython scripts can also be edited from the Edit — Document Properties menu.
What about Python modules?
The same user who requested this post also asked if he needed to install anything to use IronPython. He was thinking about how users install R or TERR packages to run TERR scripts. You don’t need to install anything, although you can. There are TIBCO Community posts on how to install custom python modules if you want to use your own or other modules like numpy or pandas. You can find those here and here. If you are reading this, you probably aren’t that far along. But that’s okay! There’s plenty to learn. This should get you started implementing IronPython in your own DXPs.
Welcome to my second IronPython post this week. If you missed the first one, start here. The first post introduced the code and provided information on how to modify it for other use cases. This post builds on that by focusing on navigating the Spotfire API reference. The next post will then explain how and why the code is written. A screenshot of the code is shown below, and then we’ll dive into the API.
This code is grabbing a particular filter in the filter panel (a checkbox filter) and unchecking all the boxes.
Walking Thru the API
I am going to walk thru the API one reference at a time. Note, I am NOT going to explain exactly what the code is doing (yet). This section will show you where to find the API references, which in turn will show you how to navigate the API. The next post will explain what the code is doing and why.
Lines 1, 3, 5
Lines 1, 3, and 5 import namespaces and classes from the Spotfire.Dxp.Application.Filters namespace.
Line 1 import Spotfire.Dxp.Application.Filters as filters
Line 1 imports the Spotfire.Dxp.Application.Filtersnamespace. If you are in the API reference, this namespace is 8 lines down at the highest level of the hierarchy. It is also mentioned/linked in the Spotfire.Dxp.Applicationnamespace as shown below. As you can see in the API reference, this namespace works with filters and filtering schemes in the filter panel. That is exactly what we are doing – modifying a filter.
Line 3 import Spotfire.Dxp.Application.Filters.CheckBoxFilter
Line 5 from Spotfire.Dxp.Application.Filters import FilterTypeIdentifiers
Lines 3 and 5 import CheckBoxFilter and FilterTypeIdentifiers, which are classes in Spotfire.Dxp.Application.Filtersnamespace. Once the class has been added, the developer can reference a class’ properties, methods, fields, etc.
Line 7 myPanel = Document.ActivePageReference.FilterPanel
Line 7 references the Documentclass, which is a class in the Spotfire.Dxp.Applicationnamespace. The developer didn’t import this namespace because Spotfire imports most of it by default. ActivePageReference is a property of the Documentclass. Now, the ActivePageReference gets or sets the currently active Page. If you are in the API reference and click on Page as circled below, you’ll see this then takes you to the Pageclass, where Filter Panel is a property.
Line 9 myFilter = myPanel.TableGroups.GetFilter("BudgetNode")
Line 7 essentially ends in the Filter Panel. Then, Line 9 calls the TableGroupsproperty, which is in the FilterPanelclass under the Spotfire.Dxp.Application.Filtersnamespace. The code also uses the GetFiltermethod from the FilterGroupclass.
Line 11 myFilter.FilterReference.TypeId = FilterTypeIdentifiers.CheckBoxFilter
Line 11 references a lot of the API, and this one was a bit confusing at first because I didn’t understand how to move from the filter panel to the actual filter. TIBCO support helped by showing me this flow chart. In order to access filters in the filter panel, you must use the TableGroup, even if there is only one table in your DXP. TableGroup is the path to the FilterHandle, which accesses the filter.
Now, when I was working thru the API, I saw references to the FilterSubGroup, and I wasn’t sure if it was referring to creating a subgroup in the filter panel (select filter in filter panel, right-click, New group) or if it was a different API reference. Groups in the filter panel would be accessed via TableGroup.SubGroups, but they don’t have to be there.
So, line 11 hits three different classes and multiple properties. First, it references the FilterReferenceproperty in the FilterHandle class. If you go to the FilterHandleclass, you will find the FilterReferenceproperty in the API. This gets the Filter that fits this FilterHandle reference. If you click on Filter (as circled below), it takes you to the Filterclass. Then the code uses the TypeId property in the Filterclass. That value should equal the CheckBoxFilterfield from the FilterTypeIdentifersclass.
Line 13 checkBoxFilter = myFilter.FilterReference.As[filters.CheckBoxFilter]()
Line 13 is just as busy as Line 11. It hits the FilterReferenceproperty in the FilterHandleclass. That connects with the Asmethod from the Filterclass. Remeber, Line 1 created the filters object, which was equal to the Spotfire.Dxp.Application.Filtersnamespace, so filters.CheckBoxFilter = Spotfire.Dxp.ApplicationFilters.CheckBoxFilter, which is a reference to the CheckBoxFilterclass.
Line 15 checkBoxFilter.IncludeEmpty = False
This line uses the IncludeEmptyproperty, which is in the CheckBoxFilterclass. Can you find this on your own in the API?
Line 17 for value in checkBoxFilter.Values:
Here, the code references the Valuesproperty of the CheckBoxFilterclass. What about this one? Can you find it on your own in the API?
Line 19 checkBoxFilter.Uncheck(value)
Line 19 uses the Uncheckmethod of the CheckBoxFilterclass. Although, note that in this line, checkBoxFilter is an object, not a reference to the class.
Now you know where all of the API references came from! Before wrapping up, I want to mention that I had to search the API quite a bit. Now, searching would be easy if the API named all of the properties, methods, fields, classes, etc uniquely. If my search returned an API result, I would know it is the right reference because there is only one. Unfortunately, the API is not written that way. Classes are distinctly named, but other pieces of syntax can have duplicates. For example, Title is a property of Page, Panel, MapChart, and all other visualization types represented as classes. Keep that in mind as you are exploring (and checking my work).
Summary of API References
If you want to walk thru everything again, here is a summary of the namespaces, classes, properties, methods, and fields used in this code.
Unfortunately, there’s a whole lot I haven’t addressed yet. For example, what if you don’t know the difference between a namespace, property or class? Don’t worry, I’ll get there in a future post. What if you don’t understand why each piece of the API was called. I’ll get there too. This learning process is a long game. The next post explains how the code is written.
This is going to be a big week of posts, three posts to be exact. I’m heading to Houston, which means a lot of time on planes and time for writing. The foundation for the posts is a piece of IronPython code that checks and unchecks the checkboxes in a filter. I needed code to do this for a project I was working on last week. A Google search turned up this TIBCO community link. The second response was exactly what I needed.
Now, I’m on a bit of a learn IronPython kick, and I wanted to understand to how this was written. I started by looking up each piece of API in TIBCO’s API reference. In the past, the API reference read like Greek to me. But walking thru the API one piece at a time, combined with the other 16 hours I’ve put into IronPython, really helped me understand what each line of code was doing. I got really excited and started writing it up. Per usual, it was too long. So, now I’m on a plane breaking it down into 3 posts, for ease of consumption. Here’s how it’s going to go.
Post 1 (this post) takes a high-level look at the code and explains how to modify it for different use cases. This will familiarize you with the code. I’ll also talk about objects versus syntax to build on the post I wrote last week on Learning IronPython.
Post 2will talk about the API in detail by explaining where each piece of the API is and how to navigate to it.
Post 3 (include link) will explain what the code is doing and why. This is something I have shied away from in the past because I didn’t have the understanding to do so. I’m getting there one code snippet at a time, and you’re coming with me.
We are going to get to the code, but first, I want to clarify my requirements.
Ideally, I wanted one piece of code to check and uncheck all the checkboxes. The TIBCO article presented two code snippets, so I created two buttons with two sets of code, one for checking and another for unchecking. I’m going with this even though a slightly better iteration would be one code snippet with “IF” logic that looks to see if the boxes are checked and if they are, it unchecks them and vice versa. I’m going with two buttons for now. Gotta walk before you run.
Now, you can copy and paste the code from the TIBCO link, so I won’t include that here. I will show my screenshots so there is no question about spacing.
Next, let’s talk about code syntax versus object.
Last week, I discussed how to figure out if a particular word in the code is an object the developer created or part of the syntax. I suggested running a simple test by changing the name of the object. If the code still runs, it’s an object, not syntax. To build on that though, the equals sign usually signals an object. Keywords, like “as”, “for”, and “in” are also indicative an object is coming.
For example, the words circled in the screenshot below were created by the developer. They could be any word the developer chooses. Also, all of the other references to Filters are uppercase, indicating they are API references. That’s not to say that objects can’t be uppercase, but if all the API references are in uppercase, it makes sense to name objects in lowercase.
Now, I don’t want to just show you which parts of the code are objects. I want to explain why the developer created the objects. Generally speaking, developers create objects to make referencing things in code easier. I’ll use “filters” as an example. This object is only used one other time after creation in line 13. So, the two lines of code shown below illustrate the difference with and without the object.
Next, let’s talk about how to modify this code for your own use cases.
Modifying for Different Scenarios
There are four modifications you might want to make to this code.
Obviously, you’ll want to change the column name to your own columns of data.
If there is more than one table in your analysis, you will most likely need to change the TableGroup index.
You might want to account for Empty values.
You might need to check and uncheck more than one filter.
First & Second Modifications
The first modification is the simplest to make. Just change the column name circled below. Next, the code “myPanel.TableGroup is telling Spotfire which table to get BudgetNode from. The number four is an index of table positions in the filter panel. The index values start at zero. My table is the 5th in the list as shown in the second screenshot below, so I need 4 in the index to get the column from the correct data table in the filter panel.
The third modification relates to what happens when there are empty values in the filter.
If the Boolean value is set to False, (Empty) will not be unchecked when the button is clicked. If you want (Empty) to be unchecked, set the value to True.
Line 15 checkBoxFilter.IncludeEmpty = False
Line 15 checkBoxFilter.IncludeEmpty = True
If you need to uncheck multiple filters, copy and paste lines 9 thru 19. Then modify the myFilter, checkBoxFilter, and value objects. Rename them something like mFilter1, checkBoxFilter1, and value1 so that they are their own distinct objects and don’t conflict with previously defined objects. Now, I am going to walk thru the API via this code example.
Lastly, you might have noticed the post introduced two code snippets but only followed one thru to this conclusion. The code snippets are similar enough that you should be able to make the same modifications described for both pieces of code.
The next post is going to dive deep into the API. Be ready to follow along. Click here to jump straight to it.
Ever since I learned what IronPython was and what it could do in Spotfire, I’ve had a “goal” to learn IronPython. I put the word goal in air quotes because learning IronPython isn’t a goal. Or at least, it’s not a very good one because it’s not measurable. How do I know when I’ve accomplished it? And that’s not to say I haven’t learned any IronPython over the years. Clearly, I have. I write about it a lot, but I aspire for deep knowledge.
The struggle was knowing how to compartmentalize learning IronPython. How could I break it down into something measurable? Finally, in October I commited to 100 hours of IronPython. Now, that’s measurable. The amount of time is somewhat arbitrarily. However, I expected that if I spent 100 hours on IronPython, I would know significantly more than when I started and be able to explain it other people.
So, did that happen? OF COURSE! Heck, I’m only 16 hours in, and I understand so much more now than I did 16 hours ago. Now, you might be saying — Wait, you set the goal in October, and you’re only 16 hours in? Yes. That is correct. I do have a day job and this awesome blog and HOLIDAYS. Stuff happens, but life (and my career) is a long game, so no giving up.
That is my somewhat long-winded explanation to this post. I know all users struggle with applying IronPython in Spotfire, and I want to make that easier on you. Since it’s a bit of an unknown path for me, I can’t break this down into a series like I have other topics. You’ll just see Learning IronPython posts now and again.
This post in particular is going to cover the following…
An explanation of why learning IronPython is so hard
My initial learning goals
My learning methods
Some syntax and structure via a code example
Please feel free to comment if you see places where I am going awry, but be nice.
Why is learning IronPython so hard?
When learning a new coding language, many users look for books or online tutorials. If you google “learn IronPython”, the results are surprisingly sparse. The first result is to a set of documentation, and the second is Quora. Anytime Quora is in your top results, you are in trouble. The fourth reference is in the UK….you see where this is going.
If you search Amazon for books on IronPython, you get the results shown below. After 3 or 4 books, the results shift to books about Python. None of the results look suitable for beginners. Strike two.
At some point, you have to ask, what is IronPython? IronPython.net will tell you…
IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET FrameworkIronPython.net
That might lead you to think that you need to learn Python, and that would be helpful, but Wiki will tell you…..
IronPython is written entirely in C#, although some of its code is automatically generated by a code generator written in Python.Wikipedia
The hamster wheel is now starting to turn, and perhaps you remembered that Spotfire extensions are also written in C#, and you have seen a reference to C# in the TIBCO API documentation, as shown here.
Wait….you mean I should actually be learning C#??? Yup. Now, things get a lot easier.
Now, it may seem like a bit of backtracking, but I want to explain what I set out to learn. If you’ve read the blog for any length of time, you know I’ve posted IronPython code snippets before. How can I do that but not really know IronPython? Easy…I learn by looking at code online, breaking it down bit by bit, and learning by doing (i.e modern learning). However, without a solid understanding of the underlying architecture, that method is limited. Thus, my primary goals for the first 10 hours were…
Find better resources
Get an understanding of IronPython structure
Get an understanding of the Spotfire API
Apply that understanding in Spotfire code examples
It turns out, that took 16 hours, not 10. I had to put in 16 hours of learning before I felt confident enough to write this post.
I started my learning process by evaluating what I knew and didn’t know. Right before I kicked off this journey, I learned about The Spotfire IronPython Quick Reference. This is an amazing website for learning IronPython for Spotfire. I started this code snippet from the Quick Reference to make a basic assessment.
As a result, here are a few questions that came up.
Why is there no reference to the namespace? Most IronPython that I’ve seen before always starts with the “import something” command.
IronPython is an object-oriented programming language. How do I differentiate between developer named objects and syntax that was part of the code structure?
I know references to the API should be capitalized. If that’s true why are “page” and “visual” in page.Visuals and visual.Title lowercase?
I can see that “Pages” is a property in the Document class. “Visuals” and “Title” are properties in the Page class. “Title” is also a property in the Visual class. Thus, why does the code only call the Document class with “Document.Pages”? There is no reference to the Page class or the Visual class. (This question might be difficult if you aren’t familiar with traversing the Spotfire API).
So, let’s answer those questions.
Syntax & Structure
Why is there no reference to the namespace?Most of the classes in the Spotfire.Dxp.Application namespace load by default, so you don’t have to import. There are some exceptions like DocumentSaveSettings and DocumentOpenSettings. Thank you TIBCO support for that answer.
How do I differentiate between developer named objects and code structure? Simple. You test it. In the code snippet provided, you might wonder if “page” in “for page in Document.Pages:” is part of the code structure or an object. Replace “page” with any other word. If the code runs, it was a named object. If it fails, it’s part of the code structure. It’s also lower case, so that is a hint. All references to the API are in uppercase.
Why are “page” and “visual” in “page.Visuals” and “visual.Title” not capitalized? They are objects, not references to the API. They could be any word.
I can see that “Pages” is a property in the Document class. “Visuals” and “Title” are properties in the Page class. “Title” is also a property in the Visual class. Thus, why does the code only call the Document class with “Document.Pages”? To follow along with the answer to this, go to the Spotfire API reference. Open the Spotfire.Dxp.Application namespace (first namespace in the API). Click on the Document Class. The intro to the Document Class says this:
A document opened in a running instance of TIBCO Spotfire is referred to as an Analysis Document. The document not only contains a series of metadata information (see DocumentMetadata), but it also contains references to the data itself (see DataManager), and to various other components being part of the document, such as pages, filterings, bookmarks, etc. As soon as data has been opened in TIBCO Spotfire, an instance of this class can be accessed through the Document property of the AnalysisApplication. This is regardless of whether the data was opened through the user interface or programmatically. TIBCO Support
There’s a lot going on in that statement. To explain it, go to the Spotfire.Dxp.Application namespace and click on the line below it that says AnalysisApplication Class.
You can see that Document is, in fact, a property of the namespace, as indicated in the description. If you click on Document, it jumps to the Document class. How does that answer the original question? We’ll get there. I just wanted to start with an explanation from the API and show you how navigating it works. Our original question asked about the Document class, which is where you should be in the API reference if you are following along. So….
Pages is a property of the Document class. The code “for page in Document.Pages:” will get the pages of the document. More specifically, it is getting a collection of pages. If you click on the Pages property in the Document class, it will take you to the screen show below. There, you’ll note that the Type = PageCollection. The pages of the document are part of a PageCollection. Now, click on PageCollection.
Now you jump down to the PageCollection class, which is below the Page Class and essentially because you are in the PageCollection Class you also have access to the properties of the Page class. Title and Visuals are both properties of the Page class (as noted a while ago in the post). The code only calls the Document class because you can access Title and Visuals by navigating the hierarchy as we just did.
As someone new to understanding the structure, I find this a bit confusing. Because the API reference is organized like a tree, I expect to navigate it in a certain way, and that is clearly not how it should be navigated. Hopefully, this will make more sense in another 10 hours or so.
Once I started searching for C# references, I found several very good tutorials worth sharing. To keep everything straight, I started saving tutorials and links by C# structure/syntax so I could have them handy and not have to search each time. I’ve made the list available for you in this post. I will maintain this as I move thru learning. Please feel free to comment with other links, and I’ll add them.