solara icon indicating copy to clipboard operation
solara copied to clipboard

[Feature Request] solara DataFrame should handle pandas Styler

Open chaffra opened this issue 2 years ago • 6 comments

If you have a styled df = pandas.DataFrame().style, you can pass it as sdf = solara.DataFrame(df.data) but you loose the styling that was applied to df. So if columns were hidden by the styler, they are displayed again. It would be good for solara.DataFrame to reapply the style before when displaying sdf in a notebook for example.

chaffra avatar May 23 '23 15:05 chaffra

Hi,

i'm not sure we should support the .style attribute in our DataFrame component, but maybe https://solara.dev/api/display is what you want display(df) in a component should get you the same output as in the notebook. Is that want you need?

maartenbreddels avatar May 26 '23 18:05 maartenbreddels

I don't know which feature from pandas Styler @chaffra wants, but in my case I would like to remove the index column or alternatively to set as index another column (in the example below the column "token ID"). Any idea on how to do that? Screenshot 2023-10-27 101757

alonsosilvaallende avatar Oct 27 '23 08:10 alonsosilvaallende

@alonsosilvaallende I was looking into getting a sensible first column as well. The first step I used was to set the token ID as index in pandas df = df.set_index("Token ID"). Then the default display method of pandas will already use this column as an index instead. Solara dataframes don't support this view yet, but I saw that someone has created a pull request to support it here: #526

GijsVermarien avatar May 03 '24 12:05 GijsVermarien

The index is a seperate issue, and indeed is worked on in #526, which I should review.

cc @dkaznachey

An example on how to do styling:

# based on https://pandas.pydata.org/pandas-docs/version/1.1/user_guide/style.html

import solara
import seaborn as sns
import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan

cm = sns.light_palette("green", as_cmap=True)
df_styled = df.style.background_gradient(cmap=cm)
df_styled


@solara.component
def Page():
    # using Jupyter's display mechanism, just as in the notebook
    display(df_styled)

Run and edit this code snippet at PyCafe

Preview: preview

maartenbreddels avatar Aug 14 '24 13:08 maartenbreddels

I would still like for the DataFrame component to support style. In our application we use the DataFrame to allow multiple user actions, which would not be available via display.

dkaznachey avatar Aug 14 '24 19:08 dkaznachey

Yes, in that case you'd want to have more flexibility indeed. Our longer term plan is to separate the DataFrame into a base component that is more level and would allow things like styling. Our DataFrame component would then use that component, and is simply a convenience layer on top. See also a discussion here: https://github.com/widgetti/solara/issues/599

maartenbreddels avatar Aug 15 '24 12:08 maartenbreddels