Python: display result Table after calculation

Is there a way to display the result table by default?
I have created it but it is hidden by default, I must go to Data module and open the eye manually. I want to do this in my python script. The table shall be displayed in the tables widget.

If I try to force by widget logic this happens

>>> slicer.qMRMLTableWidget().setMRMLTableViewNode(tablenode)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: method requires a vtkMRMLTableViewNode, a vtkMRMLTableNode was provided.

And there is nothing like setMRMLTableNode.

I also tried the following:

>>> tableview = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableViewNode')
>>> tableview.SetTableNodeID(tablenode.GetID())
>>> slicer.qMRMLTableWidget().setMRMLTableViewNode(tableview)
>>> slicer.mrmlScene.AddNode(tableview)
(MRMLCorePython.vtkMRMLTableViewNode)0x124ee34c8

but still nothing

This example shos how to do this with plots (https://gist.github.com/hherhold/0492146f70ef0fc511b7b21410264481) but I want to do it with tables
I tried the equivalent procedure but it does not work: The tableWidget.mrmlTableViewNode() method does not exists. It has however the tableWidget.setMRMLTableViewNode(), but no way to get it.
What am I missing here?

This was harder than I thought, but finally came to a working code:

    # Creation of your table
    resultsTableNode = (...)
    # Create the layout manager to show the table
    layoutManager = slicer.app.layoutManager()
    layoutTable = slicer.modules.tables.logic().GetLayoutWithTable(layoutManager.layout)
    layoutManager.setLayout(layoutTable)
    # Create the table widget and assign the table
    tablewidget = layoutManager.tableWidget(0)
    tableview = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableViewNode')
    tableview.SetTableNodeID(resultsTableNode.GetID())
    tablewidget.setMRMLTableViewNode(tableview)

Hmm, If I call this repetitively with another table it does not work, it creates more tableviews but the tablewidget is not refreshed. So I keep researching

Deep, very deep in the Slicer source code, there is the SegmentStatistic module which contains the answer, it is obscured

def showTable(self, table):
    """
    Switch to a layout where tables are visible and show the selected table
    """
    currentLayout = slicer.app.layoutManager().layout
    layoutWithTable = slicer.modules.tables.logic().GetLayoutWithTable(currentLayout)
    slicer.app.layoutManager().setLayout(layoutWithTable)
    slicer.app.applicationLogic().GetSelectionNode().SetActiveTableID(table.GetID())
    slicer.app.applicationLogic().PropagateTableSelection()
2 Likes

You can also use slicer.modules.plots.logic().ShowChartInLayout(plotChartNode) as shown in this example in script repository: https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_histogram_plot_of_a_volume

What do you mean? Is this also working with table nodes?

You are right, it’s only for charts, the same kind of “show” utility function could be added to tables module logic.