Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lukasmasuch
Copy link
Collaborator

@lukasmasuch lukasmasuch commented Apr 25, 2025

Describe your changes

Fix a bug (regression?) with data editor that prevents adding added rows to the result dataframe if existing rows got deleted. The root cause is that deleting rows might cause the index to be changed from range index to integer index, breaking our automatic increment logic when adding new rows.

GitHub Issue Link (if applicable)

Testing Plan

  • Added unit tests.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Contributor

snyk-io bot commented Apr 25, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@github-actions
Copy link
Contributor

github-actions bot commented Apr 25, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-11183/streamlit-1.44.1-py3-none-any.whl
🕹️ Preview app pr-11183.streamlit.app (☁️ Deploy here if not accessible)

@sfc-gh-lmasuch sfc-gh-lmasuch added security-assessment-completed Security assessment has been completed for PR change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Apr 25, 2025
@lukasmasuch lukasmasuch requested a review from Copilot April 25, 2025 10:06
@lukasmasuch lukasmasuch marked this pull request as ready for review April 25, 2025 10:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue in the data editor where adding rows fails after row deletions change the dataframe index type, by updating the auto-increment logic.

  • Added comprehensive tests covering various index types (range, non‐contiguous integer, datetime, and empty dataframes).
  • Updated _apply_row_additions to correctly handle different index types and log warnings when necessary.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/tests/streamlit/elements/data_editor_test.py New tests verifying row additions for various index scenarios and the correct warning for unsupported cases
lib/streamlit/elements/widgets/data_editor.py Refactored row addition logic to support auto-increment behavior for range and integer indexes and to prioritize explicit index values for non-range types


# This is only used if the dataframe has a range or integer index that can be
# auto incremented:
index_stop: int | None = None
Copy link
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby Apr 25, 2025

Choose a reason for hiding this comment

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

I wonder if this logic 308 - 363 would actually be simplified by having two function for each case and conditionally calling one based on the isInstance checks.

Then, maybe you could create a little function for creating the new row that could be shared between the functions. It could also return the current index even though that is only used by case 1.

Copy link
Collaborator Author

@lukasmasuch lukasmasuch Apr 25, 2025

Choose a reason for hiding this comment

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

I extracted one part of the function into a dedicated function for simplification. But I think the rest is a bit more complicated to move into dedicated functions since it is a bit entangled. For example, if it's a pd.Index with integer type, we want to use the auto-increment used by range index, but only if the user hasn't set an index value. And if it is a range index, we always want to use auto-increment, even if the UI has provided an index value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit easier to read, thanks! I guess the rest is reflective of all these different scenarios.

@lukasmasuch lukasmasuch merged commit 2da1489 into develop Apr 25, 2025
34 checks passed
@lukasmasuch lukasmasuch deleted the fix/data-editor-range-index-handling branch April 25, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data Editor Doesn't Return New Rows When Used in a Form and Other Records Have Been Modified and Deleted

4 participants