Skip to content

Commit

Permalink
Configure non-range indices as required for editing (streamlit#7481)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasMasuch authored and eric-skydio committed Dec 20, 2023
1 parent 3cef9bf commit 9a2f874
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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

0 comments on commit 9a2f874

Please sign in to comment.