# Python: display result Table after calculation

**URL:** <https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757>\
**Category:** Support\
**Created:** [February 13, 2019, 4:07pm UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757 "2019-02-13T16:07:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 13, 2019, 4:07pm UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/1 "2019-02-13T16:07:40Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 13, 2019, 6:09pm UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/2 "2019-02-13T18:09:49Z")

</div>

I also tried the following:

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

```

but still nothing

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 14, 2019, 9:47am UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/3 "2019-02-14T09:47:46Z")

</div>

This example shos how to do this with plots ([https://gist.github.com/hherhold/0492146f70ef0fc511b7b21410264481](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?

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 14, 2019, 10:08am UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/4 "2019-02-14T10:08:05Z")

</div>

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

```python
    # 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)

```

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 14, 2019, 10:37am UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/5 "2019-02-14T10:37:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 14, 2019, 11:24am UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/6 "2019-02-14T11:24:36Z")

</div>

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

```python
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()

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 14, 2019, 1:29pm UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/7 "2019-02-14T13:29:26Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_histogram_plot_of_a_volume)

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [February 14, 2019, 2:46pm UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/8 "2019-02-14T14:46:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 14, 2019, 4:28pm UTC](https://discourse.slicer.org/t/python-display-result-table-after-calculation/5757/9 "2019-02-14T16:28:14Z")

</div>

You are right, it’s only for charts, the same kind of [“show” utility function](https://github.com/Slicer/Slicer/blob/e18cc7b6df0df3c6626aff8b80568ab75695f580/Modules/Loadable/Plots/Logic/vtkSlicerPlotsLogic.cxx#L107-L137) could be added to tables module logic.
