posit-dev / posit-dev/py-shiny

Display nan values in DataFrame/DataGrid

Open
#797 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

Is it possible to display nan values with a DataGrid? For example, this code is displaying a blank cell for the nan values:


import palmerpenguins
import pandas as pd
from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui

penguins = palmerpenguins.load_penguins()
# Slim down the data frame to a few representative columns
penguins = penguins.loc[
    penguins["body_mass_g"].isnull(),
    ["species", "island", "body_mass_g", "year"],
]

app_ui = ui.page_fluid(
    ui.input_select(
        "selection_mode",
        "Selection mode",
        {"none": "(None)", "single": "Single", "multiple": "Multiple"},
        selected="multiple",
    ),
    ui.input_switch("gridstyle", "Grid", True),
    ui.input_switch("fullwidth", "Take full width", True),
    ui.input_switch("fixedheight", "Fixed height", True),
    ui.input_switch("filters", "Filters", True),
    ui.output_data_frame("grid"),
    ui.panel_fixed(
        ui.output_text_verbatim("detail"),
        right="10px",
        bottom="10px",
    ),
    class_="p-3",
)


def server(input: Inputs, output: Outputs, session: Session):
    @output
    @render.data_frame
    def grid():
        height = 350 if input.fixedheight() else None
        width = "100%" if input.fullwidth() else "fit-content"
        if input.gridstyle():
            return render.DataGrid(
                penguins,
                row_selection_mode=input.selection_mode(),
                height=height,
                width=width,
                filters=input.filters(),
            )
        else:
            return render.DataTable(
                penguins,
                row_selection_mode=input.selection_mode(),
                height=height,
                width=width,
                filters=input.filters(),
            )

    @output
    @render.text
    def detail():
        if (
            input.grid_selected_rows() is not None
            and len(input.grid_selected_rows()) > 0
        ):
            # "split", "records", "index", "columns", "values", "table"
            return penguins.iloc[list(input.grid_selected_rows())]


app = App(app_ui, server)

(In the other hand, the output_table doesn't show empty cells) Is there an option for python like in the renderTable from Shiny R?:
na
The string to use in the table cells whose values are missing (i.e. they either evaluate to NA or NaN).
Thank you!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example using render.DataGrid and render.DataTable with the pandas DataFrame containing NaN values, starting at the output_data_frame entry point. Trace how missing values are rendered and define completion as displaying them in the grid with a configurable replacement string, while preserving existing table behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data, frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.