Skip to content

[WIP] fix(DataGrid): Allow columns with empty titles to be used in DataGrid #1948

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions js/data-frame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const ShinyDataGrid: FC<ShinyDataGridProps<unknown>> = ({
}
},
enableSorting,
...(colname === "" && { id: findUnusedColumnName(columns, " ") }),
};
}),
[columns, typeHints]
Expand Down Expand Up @@ -1018,6 +1019,21 @@ const ShinyDataGrid: FC<ShinyDataGridProps<unknown>> = ({
);
};

/**
* Solves the problem arising when there's more than one column with the same name (or when
* the column name is illegal, like in the case of an empty name) by returning a name with
* enough spaces appended so that it becomes unique.
* @param columns Names of existing columns.
* @param initName String to start the search with.
* @returns A string that does not exist in columns and thus can be used as column ID.
*/
function findUnusedColumnName(columns: ReadonlyArray<string>, initName = " "): string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move initName into the function as this will only hold true if the initName is " ".

If other column names can be expanded, then they could both resolve to the same " " string.

However, if we know that "" is the only one being expanded and there are no duplicate names, then this function is only called once... which is safe.

Changing to something more specific...

Suggested change
function findUnusedColumnName(columns: ReadonlyArray<string>, initName = " "): string {
function emptyColumnNameToId(columns: ReadonlyArray<string>): string {
let initName = " ";

while (columns.includes(initName)) {
initName += " ";
}
return initName;
}

function findKeysBetween<TData>(
rowModel: RowModel<TData>,
fromKey: string,
Expand Down
4 changes: 2 additions & 2 deletions shiny/www/py-shiny/data-frame/data-frame.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions shiny/www/py-shiny/data-frame/data-frame.js.map

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions tests/playwright/shiny/bugs/1844-datagrid-empty-column/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pandas as pd

from shiny import App, Inputs, reactive, render, ui

app_ui = ui.page_fluid(
ui.output_data_frame("df1"),
)


def server(input: Inputs):
df = reactive.Value(
pd.DataFrame(
{
"": [1, 2],
"A": [4, 5],
" ": [7, 8],
}
)
)

@render.data_frame
def df1():
return render.DataGrid(df())


app = App(app_ui, server)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

from playwright.sync_api import Page

from shiny.playwright import controller
from shiny.run import ShinyAppProc


def test_empty_column_name(page: Page, local_app: ShinyAppProc) -> None:
page.goto(local_app.url)

df = controller.OutputDataFrame(page, "df1")
df.expect_nrow(2)
df.expect_ncol(3)
df.expect_column_labels(["", "A", " "])

df.expect_cell("1", row=0, col=0)
df.expect_cell("2", row=1, col=0)
df.expect_cell("4", row=0, col=1)
df.expect_cell("8", row=1, col=2)
Loading