posit-dev / posit-dev/py-shiny

Dates not formatted in render.data_frame

Open
#637 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

@jcheng5 Perhaps this is expected and up to the user to format?

image

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


# Create a date range
dates = pd.date_range("20230101", periods=6)

# Create a DataFrame
data = pd.DataFrame(
    {
        "A": np.random.rand(6),  # numeric column
        "B": pd.Categorical(
            ["test", "train", "test", "train", "test", "train"]
        ),  # categorical column
        "C": dates,  # date column
    }
)

data


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.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):
    df: reactive.Value[pd.DataFrame] = reactive.Value(data)

    @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(
                df(),
                row_selection_mode=input.selection_mode(),
                height=height,
                width=width,
            )
        else:
            return render.DataTable(
                df(),
                row_selection_mode=input.selection_mode(),
                height=height,
                width=width,
            )

    @reactive.Effect
    @reactive.event(input.grid_cell_edit)
    def handle_edit():
        edit = input.grid_cell_edit()
        df_copy = df().copy()
        df_copy.iat[edit["row"], edit["col"]] = edit["new_value"]
        df.set(df_copy)

    @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 df().iloc[list(input.grid_selected_rows())]


app = App(app_ui, server)

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 supplied Python example with render.data_frame using both DataGrid and DataTable, then trace how date columns are displayed. Determine whether the current output is expected and identify any existing tests or documentation covering date formatting; done means the behavior is resolved or clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.