Omar Ahmed
Omar Ahmed

Reputation: 159

Formatting Plotly Table by row

I'm Trying to create a table visual using Plotly and SQL

I've managed to extract data in the required format and adding a summary row using SQL.

Yet I need to format that last row "Total" to be bold and in a larger font size.

is that possible using plotly in jupyter?

fig = go.Figure(data=[go.Table(
    header=dict(values=['<b>Product Group<b>','<b>Quantity Sold<b>', '<b>Sales<b>'],
                fill_color='white',
                align='center',
                font_size = 15),
    cells=dict(values=[PQS.ProductGroup, PQS.Quantity, PQS.Price],
               fill_color='white',
               align='center',
               format=[None,",d",",d"],
               suffix=[None, ' Units',' EGP'],
               font_size=12,
               ))
])

fig.show()

Image from Plotly

Trying To reach similar look to this.

Image from Tableau Report

This image is from Tableau and the reason for migration is easier automation and reporting via python

is there a way to replicate such formatting in Plotly? or should I be using another library?

Upvotes: 3

Views: 3498

Answers (1)

Derek O
Derek O

Reputation: 19545

I don't think Plotly can format individual rows of a table because all of the rows of your selected columns are being passed at once to cells.

However, one workaround would be to make a copy of your DataFrame PQS (so that the numbers in your original table are preserved if you need them), cast this new table to be object or string types, apply all of the formatting changes to these columns (including formatting numbers with decimals and commas), then add the html tag <b>...</b> to the last row prior to passing this newly formatted copy of PQS to go.Table.

For example:

import plotly.graph_objects as go
import pandas as pd

## reproduce your example with a similar dataframe
PQS = pd.DataFrame({
    'ProductGroup': [None,'Beverages','Others','Pizzas','Trattoria','Total'],
    'Quantity':[37,341,67,130,25,600],
    'Price':[1530,10097,3810,18625,4110,38172]
})

PQS_formatted_strings = PQS.copy()

for col in ["Quantity","Price"]:
    PQS_formatted_strings[col] = PQS_formatted_strings[col].map('{:,d}'.format)

PQS_formatted_strings["Quantity"] = PQS_formatted_strings["Quantity"] + ' Units'
PQS_formatted_strings["Price"] = PQS_formatted_strings["Price"] + ' EGP'

## add bold html tags to the last row
last_row = PQS_formatted_strings.iloc[-1,:].values
new_last_row = ['<b>' + entry + '</b>' for entry in last_row]
PQS_formatted_strings.iloc[-1,:] = new_last_row

fig = go.Figure(data=[go.Table(
    header=dict(values=['<b>Product Group</b>','<b>Quantity Sold</b>', '<b>Sales</b>'],
                fill_color='white',
                align='center',
                font_size = 15),
    cells=dict(values=[PQS_formatted_strings.ProductGroup, PQS_formatted_strings.Quantity, PQS_formatted_strings.Price],
               fill_color='white',
               align='center',
               font_size=12,
               ))
])

fig.show()

enter image description here

Upvotes: 2

Related Questions