Search code examples
pythonplotlyplotly-dashplotly-python

Plotly - Table not displaying properly until column moved / rerendered


I am using Plotly to display data in a simple table. I've populated in some dummy data below.

import plotly.graph_objects as go
data = [go.Table(
    columnorder = [1,2],    
    header = dict(
        values = ['<b>'+'TENOR'+'</b>'] + list(['a','b','c','d','e']),
        line_color = 'darkslategray',
        fill_color = 'royalblue',
        align = 'center',
        font = dict(color = 'white', size = 12),
        height = 20
        ),
    cells = dict(
        values = [['row1','row2','row3'],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]],
        line_color = 'darkslategray',
        fill = dict(color=['paleturquoise', 'white']),
        align = ['right', 'center'],
        font_size = 12,
        height = 20)
    )]

fig = go.Figure(data = data)
fig.show()

When I run this (Jupyter, but same result happens in a couple other IDEs too) it initially displays as:

enter image description here

Then, as soon as I move a column / manipulate it in any way, it then redisplays (properly, except the column that I've moved) as:

enter image description here

Any idea why it's not rendering correctly in the first place? I basically took this right from one of their table examples...


Solution

  • Comment out your line columnorder = [1,2,3,4,5],.

    import plotly.graph_objects as go
    data = [go.Table(
        #columnorder = [1,2],    
        header = dict(
            values = ['<b>'+'TENOR'+'</b>'] + list(['a','b','c','d','e']),
            line_color = 'darkslategray',
            fill_color = 'royalblue',
            align = 'center',
            font = dict(color = 'white', size = 12),
            height = 20
            ),
        cells = dict(
            values = [['row1','row2','row3'],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]],
            line_color = 'darkslategray',
            fill = dict(color=['paleturquoise', 'white']),
            align = ['right', 'center'],
            font_size = 12,
            height = 20)
        )]
    
    fig = go.Figure(data = data)
    fig.show()
    

    Specifying that is saying you only want to show those listed columns.

    Alternatively, change that line to columnorder = [0,1,2,3,4,5],.