Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data frame): Support basic cell styling #1475

Merged
merged 24 commits into from
Jul 1, 2024
Merged

feat(data frame): Support basic cell styling #1475

merged 24 commits into from
Jul 1, 2024

Conversation

schloerke
Copy link
Collaborator

@schloerke schloerke commented Jun 21, 2024

~~POC for styling cells via great_tables package. ~~ POC proven. great_tables support pushed to later release.

Using this approach as great_tables has generic styling support, whereas the styler component of pandas is only for pandas and great_tables is already integrated into polars. Hopefully for the next release, both py-shiny and great-tables will be leveraging narwhals ("Lightweight and extensible compatibility layer between Polars, pandas, cuDF, Modin, and more"). This way, both py-shiny gets styling support for any data frame package! (Related: #1439)


Example excerpt:

@render.data_frame
def summary_data():
    df_gt = gt.GT(df).tab_style(
        [
            gt.style.fill(color="purple"),
            gt.style.borders(color="green", style="dashed"),
        ],
        gt.loc.body("Species", [1, 2]),
    )

    # Current working code
    return render.DataGrid(
        df,
        styles=df_gt._styles,
    )
    
    # Ideal code
    return render.DataGrid(df_gt)

Screenshot 2024-06-21 at 11 11 35 AM


Update: June 27th:

While the great_tables integration was a success, it brought up problems for how render.data_frame was to interact with the GT object. We will approach this subject in a 1.1 release.

For now, the styles= parameter has been added to render.DataTable and render.DataGrid. This value can either be a list of style info objects or function (given data as a param) that returns a list of style objects. This style function will be called after any cells have been updated as styling for the whole table may change due to a single cell update.

Ex:

from palmerpenguins import load_penguins_raw

from shiny import App, Inputs, render, ui

df = load_penguins_raw()


df_styles: list[render.StyleInfo] = [
    {
        "location": "body",
        "rows": None,
        "cols": None,
        "style": {"background-color": "lightblue"},
    },
    {
        "location": "body",
        "rows": [1, 2],
        "cols": "Species",
        "style": {
            "background-color": "purple",
            "border-color": "green",
            "border-style": "dashed",
        },
    },
    {
        "location": "body",
        "rows": 2,
        "cols": ["Region"],
        "style": {"background-color": "yellow"},
    },
    {
        "location": "body",
        "rows": None,
        "cols": [4],  # "Island",
        "style": {"background-color": "red"},
    },
    {
        "location": "body",
        "rows": [False, True, False, False, False],
        "cols": [False, False, False, False, True, True],  # "Stage",
        "style": {"background-color": "green"},
    },
]

def server(input: Inputs):
    @render.data_frame
    def list_styles():
        return render.DataTable(
            df,
            selection_mode=("rows"),
            editable=True,
            # filters=True,
            styles=df_styles,
        )

@schloerke schloerke added the data frame Related to @render.data_frame label Jun 21, 2024
@schloerke schloerke added this to the v0.11.0 milestone Jun 21, 2024
@schloerke schloerke marked this pull request as ready for review July 1, 2024 21:12
@schloerke schloerke enabled auto-merge July 1, 2024 21:12
@schloerke schloerke added this pull request to the merge queue Jul 1, 2024
Merged via the queue into main with commit f6f9516 Jul 1, 2024
31 checks passed
@schloerke schloerke deleted the df-style-cell branch July 1, 2024 21:25
schloerke added a commit to machow/py-shiny that referenced this pull request Jul 2, 2024
* main:
  fix(tests): dynamically determine the path to the shiny app (posit-dev#1485)
  tests(deploys): use a stable version of html tools instead of main branch (posit-dev#1483)
  feat(data frame): Support basic cell styling (posit-dev#1475)
  fix: support static files on pyodide / py.cafe under a prefix (posit-dev#1486)
  feat: Dynamic theming (posit-dev#1358)
  Add return type for `_task()` (posit-dev#1484)
  tests(controls): Change API from controls to controller (posit-dev#1481)
  fix(docs): Update path to reflect correct one (posit-dev#1478)
  docs(testing): Add quarto page for testing (posit-dev#1461)
  fix(test): Remove unused testrail reporting from nightly builds (posit-dev#1476)
schloerke added a commit to machow/py-shiny that referenced this pull request Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data frame Related to @render.data_frame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant