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

Configure non-range indices as required for editing with st.data_editor #7481

Merged
merged 1 commit into from Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/streamlit/elements/lib/column_config_utils.py
Expand Up @@ -508,6 +508,11 @@ def apply_data_specific_configs(
# We rename it to "value" in selected cases to make it more descriptive
data_df.rename(columns={0: "value"}, inplace=True)

if not isinstance(data_df.index, pd.RangeIndex):
# If the index is not a range index, we will configure it as required
# since the user is required to provide a (unique) value for editing.
update_column_config(columns_config, INDEX_IDENTIFIER, {"required": True})


def marshall_column_config(
proto: ArrowProto, column_config_mapping: ColumnConfigMapping
Expand Down
13 changes: 13 additions & 0 deletions lib/tests/streamlit/elements/lib/column_config_utils_test.py
Expand Up @@ -491,6 +491,19 @@ def test_apply_data_specific_configs_hides_index(
else:
self.assertNotIn(INDEX_IDENTIFIER, columns_config)

def test_apply_data_specific_configs_makes_index_required(self):
"""Test that a non-range index gets configured as required."""
columns_config: ColumnConfigMapping = {}
data_df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["a", "b", "c"])
apply_data_specific_configs(
columns_config, data_df, DataFormat.PANDAS_DATAFRAME
)
self.assertEqual(
columns_config[INDEX_IDENTIFIER]["required"],
True,
f"Index of type {type(data_df.index)} should be configured as required.",
)

@parameterized.expand(
[
(DataFormat.SET_OF_VALUES, True),
Expand Down