[AdvancedLayouts] Add stretch height to st.dataframe#12632
[AdvancedLayouts] Add stretch height to st.dataframe#12632sfc-gh-lwilby merged 17 commits intodevelopfrom
Conversation
Adds height='stretch' functionality to st.dataframe, allowing it to stretch to fill its container height.
✅ PR preview is ready!
|
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
There was a problem hiding this comment.
This was not expected, but the difference is not visible to me so I don't think it indicates a style change that we would consider a regression.
There was a problem hiding this comment.
This change was not intended, but I think it seems OK cc @lukasmasuch in case having the dataframe in the center is a deliberate style choice.
There was a problem hiding this comment.
I saw this while doing some testing locally and it seems strange having the dataframe centered in the page. I think this is a good change.
There was a problem hiding this comment.
Yeah, it seems okay. But I'm not sure what I would favour. Do you know why this change happened?
There was a problem hiding this comment.
It's because of this change, which we need to make sure the inner dataframe fills the StyledElementContainer in stretch height.
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new height="stretch" option to st.dataframe that allows dataframes to fill their container height, implementing advanced layouts functionality to bring dataframes in line with other chart elements using the Height type system.
- Added
height="stretch"parameter support with backward compatibility for existing height values - Implemented flexbox-based height stretching logic that works within containers but falls back to default behavior at root level
- Added comprehensive test coverage for the new stretch height functionality
Reviewed Changes
Copilot reviewed 15 out of 25 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/streamlit/elements/arrow.py | Updated dataframe function signatures to accept HeightWithoutContent type and removed allow_stretch=False restriction |
| lib/tests/streamlit/elements/arrow_dataframe_dimensions_test.py | Added test case for height="stretch" configuration |
| frontend/lib/src/components/widgets/DataFrame/styled-components.ts | Added minHeight prop and updated styling for stretch height support |
| frontend/lib/src/components/widgets/DataFrame/hooks/useTableSizer.ts | Added stretch height logic with container measurement and root detection |
| frontend/lib/src/components/widgets/DataFrame/arrowUtils.ts | Added shouldUseStretchHeight helper function |
| frontend/lib/src/components/widgets/DataFrame/arrowUtils.test.ts | Added comprehensive tests for shouldUseStretchHeight function |
| frontend/lib/src/components/widgets/DataFrame/DataFrame.tsx | Integrated container height measurement and root context for stretch behavior |
| frontend/lib/src/components/core/Layout/FlexContext.tsx | Added isInRoot flag to track root-level positioning |
| frontend/lib/src/components/core/Block/Block.tsx | Passed isRoot prop to FlexContextProvider |
| frontend/app/src/components/AppView/AppView.tsx | Set isRoot=true for top-level app container |
| frontend/lib/src/test_util.tsx | Added isInRoot=false to test context |
| e2e_playwright/st_dataframe_dimensions.py | Added test scenarios for height="stretch" functionality |
| e2e_playwright/st_dataframe_dimensions_test.py | Updated test expectations and added stretch height snapshot test |
| e2e_playwright/st_layouts_container_various_elements.py | Added vertical stretch height container test case |
| e2e_playwright/st_layouts_container_various_elements_test.py | Added test key to snapshot exclusion list |
frontend/lib/src/components/widgets/DataFrame/hooks/useTableSizer.ts
Outdated
Show resolved
Hide resolved
| /** | ||
| * Helper function to determine if the element is configured to use stretch height. | ||
| */ | ||
| export function shouldUseStretchHeight( |
There was a problem hiding this comment.
I think the arrowUtils might not be the best place for the width-related utils. It's more intended to focus on everything related to the arrow data format. Since the methods only seem to be used within useTabelSizer, maybe it's better to just move them into the useTabelSizer.ts file.
There was a problem hiding this comment.
There are already a few other functions in arrowUtils for dimensions, to keep them all together I have moved them into a dimensionsUtils. Let me know if you don't like this grouping though.
| useStretchHeight && | ||
| measuredContainerHeight && | ||
| measuredContainerHeight > 0 && | ||
| !isInRoot |
There was a problem hiding this comment.
question: Whats the reason for isInRoot here? Can you add a comment here explaining why it should be ignore in root?
There was a problem hiding this comment.
And to simplify a bit, could we just do const useStretchHeight = shouldUseStretchHeight(heightConfig) && !isInRoot above and remove the isInRoot uses below?
There was a problem hiding this comment.
Yes, we can simplify this, updated.
I added more details on the !isInRoot. This is because if we add a height=100% here in the main container where there is no fixed height inclosing container, the dataframe ends up collapsed to the minimum height. So we use auto mode instead. Stretch height in the main container doesn't really make sense anyways since there is nothing to stretch to. If they want the dataframe to be full height that would be height="content". There are more details in the slack thread.
There was a problem hiding this comment.
And how is the dataframe shown if its wrapped in a basic container:
st.container().dataframe(...)
Also with auto?
There was a problem hiding this comment.
Yes, the default for the dataframe height is auto and for st.container the default height is "content" so your example would look the same as dataframe in the root container if the user doesn't configure any height settings.
📈 Frontend coverage change detectedThe frontend unit test (vitest) coverage has increased by 0.0200%
✅ Coverage change is within normal range. |
| data: Data = None, | ||
| width: Width = "stretch", | ||
| height: int | Literal["auto"] = "auto", | ||
| height: HeightWithoutContent | Literal["auto"] = "auto", |
There was a problem hiding this comment.
"stretch" probably also needs to be added to the docstring? And I think you can directly add this to data_editor API as well since it's using the exact same frontend. implementation
lukasmasuch
left a comment
There was a problem hiding this comment.
LGTM 👍 but I think we can add this to data_editor as well without adding additional e2e tests, just some unit tests (since it uses the exact same frontend component). That's why there is only a dimension test for st.dataframe.
Describe your changes
Adds a new
height="stretch"option tost.dataframeusing theHeighttype system for consistency with other chart elements.Changes Made:
height="stretch"parameter support to existing height parameterheight="auto"and integer height values still workheight="stretch", dataframe fills its container heightGitHub Issue Link (if applicable)
Implements #12391
Testing Plan
Screenshots/Demos:
(E2E test snapshots will show visual differences for height="auto", height="stretch", and height=400)
Additional Notes:
This extends the AdvancedLayouts initiative by adding height="stretch" support to
st.dataframe, bringing it in line with other chart elements that support theHeighttype system.Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.