Can I display only attributes of an object? If I use st.write it displays something like repr, then the docstring and then the table of the attributes. E.g., this
@dataclasses.dataclass
class Foo:
a: int
b: float
c: list
st.write(Foo(1, 2.3, [1, 2, 3, 5, 6]))
I like the table of attributes, how it displays type and value of each attribute. But I don’t want to show the repr and doc part. I find it rather distracting. Is there a way to suppress these?
Ideally there would be function like st.write_object, where one could configure what should be displayed…
Streamlit doesn’t have a built-in function to hide the repr and docstring when displaying an object. However, you can achieve the behavior you’re looking for by manually displaying the attributes of the object in a way that mimics how Streamlit shows the table of attributes.
Here’s one way to do it using a dictionary comprehension that extracts the attributes and types from a dataclass:
import streamlit as st
import dataclasses
@dataclasses.dataclass
class Foo:
a: int
b: float
c: list
# Create an instance of the Foo class
foo_instance = Foo(1, 2.3, [1, 2, 3, 5, 6])
# Extract the attributes and their types and values
attributes = {field.name: (type(getattr(foo_instance, field.name)).__name__, getattr(foo_instance, field.name)) for field in dataclasses.fields(foo_instance)}
# Display the attributes in a table format
st.write("### Object Attributes")
st.table(attributes)
Explanation:
dataclasses.fields(): This retrieves all the fields in the dataclass.
getattr(): This is used to get the value of each attribute dynamically.
type().__name__: This extracts the name of the type of each attribute.
st.table(): Displays the attributes as a table, showing both the type and the value of each attribute.
This approach will display only the attributes, without showing the repr or docstring, and it gives you full control over what is displayed.
thanks for the proposal, I have already implemented something similar…, but it does not look that nice. I just prefer how the attributes at types are formatted (and colored) when printing the whole object using st.write, this would be harder to replicate. Thats why I asked if the functionality that already is in the Streamlit could be used to display the attributes only.
Maybe I shall create a feature request on the Github? That functionality is already implemented, we just need to make it configurable (so that is omits the repr or doc).
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.