Skip to content

Conversation

sfc-gh-tteixeira
Copy link
Contributor

📚 Context

st.write() does something awesome with classes and generic object instances: it passes them to st.help() so the user can see that class's/object's docstring, member variables, methods, and much more.

However, if the user called st.write() on an instance of a Python dataclass, all that would be displayed was the class's name. This PR fixes that, by making the behavior for dataclass instances match the one described above.

  • What kind of change does this PR introduce?

    • Bugfix
    • Feature
    • Refactoring
    • Other, please describe:

🧠 Description of Changes

  • Add bullet points summarizing your changes here

    • This is a breaking API change
    • This is a visible (user-facing) change

👁️ What it looks like

Given this code...

import streamlit as st
from dataclasses import dataclass


@dataclass
class Vector:
    """A little 2D vector class."""
    x = 0
    y = 0

    def length(self):
        return x**2 + y**2


v = Vector()
v.x = 10
v.y = 20

st.write(v)

...this is what you get today

Screenshot 2023-05-25 at 16 31 48

...and this is what you get after this PR

Screenshot 2023-05-25 at 16 48 00

🧪 Testing Done

  • Screenshots included
  • Added/Updated unit tests
  • Added/Updated e2e tests - No need. This change doesn't impact any of the rendering logic beyond what's already caught in the unit test.

🌐 References

None


Contribution License Agreement

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

@sfc-gh-tteixeira sfc-gh-tteixeira requested a review from vdonato May 25, 2023 23:57
Copy link
Collaborator

@vdonato vdonato left a comment

Choose a reason for hiding this comment

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

LGTM

@sfc-gh-tteixeira sfc-gh-tteixeira merged commit 0c38057 into streamlit:develop May 30, 2023
@sfc-gh-tteixeira sfc-gh-tteixeira deleted the write_dataclass branch May 30, 2023 21:22
tconkling added a commit to tconkling/streamlit that referenced this pull request Jun 6, 2023
* develop:
  Remove tensorflow and pytorch from test requirements (streamlit#6807)
  Fix readme links (streamlit#6800)
  Update PR template to be simpler and more approachable (streamlit#6679)
  README updated with links to docs (streamlit#6780)
  Refactor: withHostCommunication -> HostCommunicationManager (streamlit#6746)
  Add python 3.11 to classifiers list. (streamlit#6786)
  Release/1.23.1 (streamlit#6777)
  We depend on typing-extensions 4.0.1 (streamlit#6776)
  Release/1.23.0 (streamlit#6773)
  Fix typo in data_editor docstring and deprecation msg: `edited_rows` -> `edited_cells` (streamlit#6770)
  Fix: Remove flaky date input calendar snapshot test (streamlit#6769)
  Fix 3218, patch pydantic in_ipython function (streamlit#6664)
  Upgrade react-range to fix memory usage of sliders (streamlit#6764)
  Make st.write pretty-print dataclasses using st.help (streamlit#6750)
  Replace curly with straightquotes in docstring examples (streamlit#6757)
eric-skydio pushed a commit to eric-skydio/streamlit that referenced this pull request Dec 20, 2023
* Make st.write pretty-print dataclasses using st.help

* Fix typo in function name..
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Mar 22, 2024
* Make st.write pretty-print dataclasses using st.help

* Fix typo in function name..
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
* Make st.write pretty-print dataclasses using st.help

* Fix typo in function name..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants