Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@matilde2004
Copy link
Contributor

@matilde2004 matilde2004 commented Mar 31, 2025

Describe your changes

When st.code is used, long words now break in the middle if no whitespace is present by using "break-word".
Additionally, "pre-wrap" ensures that existing whitespace is preserved while prioritizing natural line breaks.

GitHub Issue Link (if applicable)

Fixes #10824

Testing Plan

I used the Reproducible Code Example to test.
Here it is working: https://youtu.be/xYdI6NZxtsw?si=xEoYWDdyrCKL-MCB


Contribution License Agreement

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

When st.code is used, long words now break in the middle if no
whitespace is present by using "break-word".
Additionally, "pre-wrap" ensures that existing whitespace is preserved
while prioritizing natural line breaks.
@snyk-io
Copy link
Contributor

snyk-io bot commented Mar 31, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@sfc-gh-bnisco
Copy link
Collaborator

Hi @matilde2004 thank you very much for this contribution! This looks to be in the right direction!

The linked ticket states:

If wrap_lines=True is set in st.code, it only wraps on whitespace. That means long strings without a whitespace won't wrap.

I took a look at the e2e tests that failed, and a couple snapshot tests failed. I think this is because the changes in this PR are applying to all usages of the code component, regardless if the user set wrap_lines=True. In other words, there are some legitimate cases where we'd want white-space: pre to still apply, and others where white-space: pre-wrap should apply.

Would you be able to fix up the logic for this? Additionally, we should add some tests for this in e2e_playwright/st_code_test.py. Let me know if you have questions, thank you!

@matilde2004
Copy link
Contributor Author

Hi, thank you so much for the quick feedback! I think I can fix the logic, I’ll take a look at the tests that failled to work on a solution. I’ll also check the e2e_playwright/st_code_test.py and add some tests. I’ll reach out with an update as soon as possible!
For now, I don’t have any questions. Thanks again!

@sfc-gh-bnisco sfc-gh-bnisco self-assigned this Apr 2, 2025
@lukasmasuch lukasmasuch added change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels May 1, 2025
@lukasmasuch lukasmasuch requested a review from Copilot June 4, 2025 19:23

This comment was marked as outdated.

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot June 12, 2025 22:25

This comment was marked as outdated.

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot June 12, 2025 22:33
Copy link
Contributor

Copilot AI left a 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 mid-word wrapping in code blocks when no whitespace is present by introducing a wrapLines prop that toggles pre-wrap/break-word styling.

  • Expose a new wrapLines boolean to StyledCode and StyledPre for conditional whitespace and word-break CSS
  • Update the Streamlit React components to pass wrapLines through and disable wrapping in error/exception traces
  • Add Python examples and end-to-end Playwright tests for long single-word strings with and without wrapping

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
frontend/lib/src/components/shared/ErrorElement/ErrorElement.tsx Pass wrapLines={false} to StyledCode to preserve existing behavior in error stack traces
frontend/lib/src/components/elements/ExceptionElement/ExceptionElement.tsx Disable wrapping in exception stack traces via wrapLines={false}
frontend/lib/src/components/elements/CodeBlock/styled-components.ts Introduce StyledCodeProps.wrapLines and conditionally apply white-space/word-break styles
frontend/lib/src/components/elements/CodeBlock/StreamlitSyntaxHighlighter.tsx Forward the wrapLines prop to StyledPre
e2e_playwright/st_code_test.py Update code block count assertions and add snapshots for long single-word string tests
e2e_playwright/st_code.py Add reproducible examples of st.code with long single-word strings, with and without wrapping

sfc-gh-bnisco and others added 2 commits June 13, 2025 07:32
…ts.ts

Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
@sfc-gh-bnisco sfc-gh-bnisco added the security-assessment-completed Security assessment has been completed for PR label Jun 13, 2025
padding: 0,
whiteSpace: "pre",
wordBreak: "normal",
wordWrap: "normal",
Copy link
Collaborator

Choose a reason for hiding this comment

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

So just removing these because "normal" is already the default?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh good question! Yes, I removed work-break: normal because that is the default.

I changed word-wrap because it is not a standard and has been supplanted by overflow-wrap [MDN Docs]

Copy link
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby left a comment

Choose a reason for hiding this comment

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

Took a second look after Bob's changes and LGTM!

@sfc-gh-bnisco sfc-gh-bnisco merged commit 8a98b30 into streamlit:develop Jun 13, 2025
36 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wrap_lines in st.code doesn't wrap if there's no whitespace

4 participants