Very high CPU when displaying pandas styler

I have a streamlit app that displays a pandas styler object created from a dataframe.

It is not a particularly huge dataframe, about 300 rows, and the styler just loops over columns and highlights them good/great/improve based on their values. To generate the styler takes no time at all, but once it gets displayed on screen, the google chrome renderer starts eating massive amounts of CPU to the point where the dashboard becomes unusable exceeding 100% cpu usage just trying to scroll down the dataframe. I don’t really understand this cos I would have thought at this point, no more code is being run, and it implies it is literally struggling to just render this dataframe?

When I switch back to a dataframe instead of a styler, the performance immediately improves dramatically.

Has anyone come across this or have any ideas what might be causing this?

Here is an example of one of the styling functions.

def highlight_overall(numbers):
    highlight_list = []
    for i in numbers:
        if i >= 80:
            highlight_list.append("background-color: #99DEBB")
        elif i < 70:
            highlight_list.append("background-color: #ffbb9e")
        else:
            highlight_list.append("background-color: #fdff9e")

    return highlight_list

Here is a graph of my CPU usage when trying to scroll on the styler

@AEB26B Thanks for reporting this issue. Unfortunately, using Pandas Styler is quite inefficient compared to the plain dataframe. But I didn’t expect to have such a big load on the browser CPU. This will need a bit of investigation!

@lukasmasuch Thanks a lot. I am sure this did come across but it is specifically when you interact with the styler dataframe. Loading it/having it displayed on the page uses no cpu at all, but then the moment you try and scroll or sort or anything like that it just goes nuts. You can see that when you stop interacting, the cpu usage goes right down again.

Let me know if you need any more information from me re the code being run or the dataframe etc.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.