Skip to content
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

Pretty-print objects passed to text #52

Open
LukasJoswiak opened this issue Jan 27, 2025 · 1 comment
Open

Pretty-print objects passed to text #52

LukasJoswiak opened this issue Jan 27, 2025 · 1 comment

Comments

@LukasJoswiak
Copy link
Contributor

When building a Preswald app, I often need to debug intermediate output of Python objects or arrays. Passing an array to the text function should print it similar to how Streamlit prints objects passed to st.write.

Goal:

arr = [1, 2, 3, 4]
text(arr)

<web app prints arr in an easy to read manner>

@danlm
Copy link

danlm commented Feb 6, 2025

def text(markdown_str, size=1.0):
"""Create a text/markdown component."""
service = PreswaldService.get_instance()
id = generate_id("text")
logger.debug(f"Creating text component with id {id}")
component = {
"type": "text",
"id": id,
"markdown": markdown_str,
"value": markdown_str,
"size": size,
}
logger.debug(f"Created component: {component}")
service.append_component(component)
return component

You could probably use the pprint module here, something like:

if not isinstance(markdown_str, str):
    markdown_str = pprint.pformat(markdown_str, sort_dicts=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants