1

I have a Plotly table made from a pandas data frame. I want to create a new Plotly table every 10 rows and save that table as an image. Can anyone help? I know this is probably a simple question, but I'm pretty new to Python. Here is my current code as an example:

import plotly.graph_objects as go

import pandas as pd

import plotly.io as pio
import os


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')

index = df.index



rowEvenColor = 'white'

rowOddColor = 'lavender'



fig = go.Figure(data=[go.Table(

columnorder = [1,2, 3, 4],

columnwidth = [30,400, 40, 60],

header=dict(values=list(df.columns),

fill_color='navy',

font_color='white',

align=['center', 'left', 'center', 'center']),

cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],

fill_color = [[rowOddColor,rowEvenColor]*len(index)],

font_size=12,

height=60,

align=['center', 'left', 'center', 'left']))

])



fig.update_layout(width=1000, height=600)

fig.show()


if not os.path.exists("images"):

os.mkdir("images")

pio.write_image(fig, 'images/pythont.png', engine="orca")

1 Answer 1

1

You can loop over each 10 row chunk of your df, create your figure, and save it for each iteration of the loop. It might be good to also have some an iterator to keep track of how many figures are created, so that each .png file has a different name and isn't written over as the loop executes.

import numpy as np
import pandas as pd

import plotly.graph_objects as go

import plotly.io as pio
import os


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')

# index = df.index

rowEvenColor = 'white'
rowOddColor = 'lavender'
fig_num = 1

for _,df_chunk in df.groupby(np.arange(len(df))//10):
    fig = go.Figure(data=[go.Table(
        columnorder = [1,2, 3, 4],
        columnwidth = [30,400, 40, 60],
        header=dict(values=list(df_chunk.columns),
        fill_color='navy',
        font_color='white',
        align=['center', 'left', 'center', 'center']),
        cells=dict(values=[df_chunk.Rank, df_chunk.State, df_chunk.Postal, df_chunk.Population],
        fill_color = [[rowOddColor,rowEvenColor]*len(df_chunk.index)],
        font_size=12,
        height=60,
        align=['center', 'left', 'center', 'left']))
    ])
    fig.update_layout(width=1000, height=600)
    # fig.show()
    
    if not os.path.exists("images"):
        os.mkdir("images")

    # print("saving fig" + str(fig_num))
    pio.write_image(fig, 'images/fig' + str(fig_num) + '.png', engine="orca")
    fig_num += 1
Sign up to request clarification or add additional context in comments.

5 Comments

It worked! Thank you so much! Can you please explain this first line of the function? for _,df_chunk in df.groupby(np.arange(len(df))//10):
Sure! When we unpack the groupby in the loop, we are assigning the index to the underscore variable _, and assigning the chunk to the variable df_chunk. The underscore variable in this instance is a stand-in, and isn't used in the program.
Okay cool got it. I have one last question for you. So the reason I'm doing this is because I want to get these tables into a PDF report. This will include other charts and text. Which Python package do you recommend for doing this?
Hmm I haven't had to create PDF from Plotly charts, but I would start with the documentation to create PDF reports
Hi. I have another follow-up question. In the code above the columns in cells=dict(values=) are listed one by one. Is there a way to include all the columns from the dataframe in a single line of code instead of naming out each column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.