pythonplotlygoogle-colaboratory

Plotly table object on Colab


In colab, I generate plotly table object as a part of subplot in all graph object is in Ipywidgets.output like:

picture 1

But I couldn't see first column values. When I click the plotly bar, reset button, it shows like in :

picture 2

Code is:

from google.colab import output
output.enable_custom_widget_manager()
report_data = [[' Total Distance', '29,873,735,731'], [' Average Distance', '382.9']]
output = widgets.Output()
with output:
  fig.add_trace(go.Table(
            header=dict(values=['Summary', ' '], align='left'),
            cells=dict(values=list(zip(*report_data)), align='left'),
        ), row=1, col=1)
  fig.add_trace(go.Table(
            header=dict(values=['Summary', ' '], align='left'),
            cells=dict(values=list(zip(*report_data)), align='left'),
        ), row=1, col=2)
  fig.show(renderer="colab")
display(output)

I tried different browsers, also I gave cell height parameter 35, didn't worked. If I generate table alone, there is not problem. But with output it is not seen normal, if I download as a png, it shows.


Solution

  • You shouldn't need to use widgets.Output() (which is likely the cause of the issue), fig.show() should just work with the proper renderer ("colab"), but you can also set it explicitly.

    Regarding the cell height, you can set it in the cells dict.

    report_data = [[' Total Distance', '29,873,735,731'], [' Average Distance', '382.9']]
    
    fig = make_subplots(
        rows=1,
        cols=2,
        specs=[
            [{"type": "table"}, {"type": "table"}]
        ]
    )
    
    fig.add_trace(
        go.Table(
            header=dict(values=['Summary', ' '], align='left'),
            cells=dict(values=list(zip(*report_data)), align='left', height=35)
        ),
        row=1, col=1)
    
    fig.add_trace(
        go.Table(
            header=dict(values=['Summary', ' '], align='left'),
            cells=dict(values=list(zip(*report_data)), align='left', height=35)
        ),
        row=1, col=2
    )
    
    fig.show(renderer='colab')
    

    For a 2x2 subplots grid, adapt row, col and specs according to the trace types you want to use at each row/col pair, eg. :

    fig = make_subplots(
        rows=2,
        cols=2,
        specs=[
            [{"type": "scatter"}, {"type": "table"}],
            [{"type": "scattermapbox"}, {"type": "bar"}]
        ]
    )