widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.9k stars 140 forks source link

[Feature Request] solara DataFrame should handle pandas Styler #110

Open chaffra opened 1 year ago

chaffra commented 1 year ago

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.

maartenbreddels commented 1 year ago

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?

alonsosilvaallende commented 1 year ago

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
GijsVermarien commented 6 months ago

@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

maartenbreddels commented 2 months ago

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

dkaznachey commented 2 months ago

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.

maartenbreddels commented 2 months ago

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