-
Notifications
You must be signed in to change notification settings - Fork 4k
Support passing list of pydantic objects as dataframe-like structure #13348
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
Conversation
✅ 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. |
✅ PR preview is ready!
|
|
@cursor review |
There was a problem hiding this 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 adds support for passing lists of Pydantic model instances as dataframe-like structures in Streamlit. The implementation treats lists of Pydantic models similarly to lists of dictionaries, converting them to pandas DataFrames using either the Pydantic v2 model_dump() method or the v1 dict() method for backward compatibility.
Key Changes:
- Added a private helper function
_is_list_of_pydantic_models()to detect lists containing Pydantic model instances - Extended
convert_anything_to_pandas_df()to serialize lists of Pydantic models to DataFrames - Updated
determine_data_format()to classify lists of Pydantic models asLIST_OF_RECORDS
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/streamlit/dataframe_util.py |
Added detection and conversion logic for lists of Pydantic models, following the existing pattern used for Snowpark row lists |
lib/tests/streamlit/data_test_cases.py |
Added integration test case for lists of Pydantic models to the shared test suite, ensuring coverage across all dataframe-accepting components |
📈 Frontend coverage change detectedThe frontend unit test (vitest) coverage has increased by 0.0500%
🎉 Great job on improving test coverage! |
📉 Python coverage change detectedThe Python unit test coverage has decreased by 0.0507%
💡 Consider adding more unit tests to maintain or improve coverage. Coverage by files
|
lib/streamlit/dataframe_util.py
Outdated
| try: | ||
| first_element = next(iter(data)) | ||
| if has_callable_attr(first_element, "model_dump"): | ||
| return pd.DataFrame([item.model_dump() for item in data]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lukasmasuch would it be better to use model_dump(mode='json') here. There's a bug in st.dataframe where it incorrectly displays small Decimal values but it's solved when you do mode='json'.
from decimal import Decimal
import streamlit as st
from pydantic import BaseModel
class User(BaseModel):
id: int
name: str
is_active: bool
decimal_val: Decimal
users = [
User(id=5, name="Eve", is_active=True, decimal_val=Decimal("-0.01")),
]
st.write("Model dump python mode")
st.dataframe([user.model_dump() for user in users])
st.write("Model dump json mode")
st.dataframe([user.model_dump(mode="json") for user in users])
st.write("Actual value: Decimal('-0.01'):", Decimal("-0.01"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, good feedback. I changed it to json mode 👍
Describe your changes
Add support for passing a list of pydantic objects to
st.dataframe,st.data_editor, charts, options widgets, and wherever else we support dataframe-like structures. 32% of Streamlit apps have pydantic as part of their dependencies and we already support single pydantic objects.GitHub Issue Link (if applicable)
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.