-
Notifications
You must be signed in to change notification settings - Fork 4k
[feat][TextAlignment] Adds text_alignment parameter to markdown elements #13036
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!
|
80ad6f9 to
32d103b
Compare
0676383 to
06a7ab5
Compare
32d103b to
4a924a7
Compare
06a7ab5 to
48076e4
Compare
48076e4 to
990df6b
Compare
4a924a7 to
8f3d689
Compare
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0000%
✅ Coverage change is within normal range. |
990df6b to
23623d2
Compare
8f3d689 to
11ff345
Compare
23623d2 to
263f50d
Compare
11ff345 to
60290c2
Compare
263f50d to
30ee399
Compare
60290c2 to
18b6951
Compare
30ee399 to
3f12c77
Compare
18b6951 to
27886e4
Compare
42a9d74 to
30c8a54
Compare
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.
I see in these snapshots that the help tooltip is now inline with the hello world text in the sidebar. This is expected with the changes to move this tooltip inline and looks better to me.
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.
Lot's of these snapshot updates are due to changes in how the margins collapse/don't collapse due to adding width: 100% on the inner container to allow for the text alignment to work.
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.
Tooltip colouring is changed because it is now inline with the caption. Jess/Johannes prefer the new version.
ada9c9d to
54a05f4
Compare
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 a text_alignment parameter to st.markdown and st.caption elements, enabling horizontal text alignment with options for "left", "center", "right", and "justify". The implementation includes comprehensive Python unit tests, E2E tests, and frontend changes to support the new alignment feature. Note that while text is aligned, tables and lists within markdown remain left-aligned by design.
Key Changes
- Added
text_alignmentparameter to bothst.markdownandst.captionwith validation - Updated frontend rendering logic to handle help icons inline using a new
:help[]directive - Modified styling to ensure lists and tables remain left-aligned while text respects alignment
- Added comprehensive test coverage for all alignment types
Reviewed changes
Copilot reviewed 8 out of 210 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
lib/streamlit/elements/markdown.py |
Added text_alignment parameter to markdown() and caption() functions with documentation |
lib/tests/streamlit/elements/markdown_test.py |
Added parameterized unit tests for text alignment validation |
frontend/lib/src/components/elements/Markdown/Markdown.tsx |
Refactored help icon rendering to use inline directive approach |
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx |
Added CustomHelpIcon component and :help[] directive handler |
frontend/lib/src/components/shared/StreamlitMarkdown/styled-components.ts |
Added CSS rules to force left-align for lists/tables while allowing text alignment |
frontend/lib/src/components/shared/TooltipIcon/styled-components.ts |
Fixed LaTeX help icon width calculation for better alignment |
e2e_playwright/st_markdown.py |
Added comprehensive test cases for all alignment types with help tooltips |
e2e_playwright/st_markdown_test.py |
Added E2E test functions for text alignment verification |
e2e_playwright/__snapshots__/**/*.png |
Updated snapshot images (binary files) |
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Outdated
Show resolved
Hide resolved
e2e_playwright/__snapshots__/linux/st_divider_test/st_divider[light_theme-chromium].png
Show resolved
Hide resolved
...wright/__snapshots__/linux/st_markdown_test/st_caption-width_stretch[dark_theme-firefox].png
Show resolved
Hide resolved
| table: { | ||
| // Add some space below the markdown tables | ||
| marginBottom: theme.spacing.lg, | ||
| // Prevent double borders |
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.
nit: I think having this comment above borderCollapse might still be good to have
| const nodeName = String(node.name) | ||
|
|
||
| // Handle help icon directive (:help[tooltip content]) | ||
| if (nodeName === "help") { |
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.
question: in theory, this is also something a user could place in the markdown for adding these help tooltips to any position in the markdown?
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.
Yes, that's right. If someone discovers this they could use it to add help tooltips in other positions. I don't think we should document this as officially supported, but it seems OK if someone uses it.
I just realized I should put this in a separate function as well.
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.
I actually think this might be a nice aspect to document and officially support :) but lets wait for some opinions (cc @jrieke , @sfc-gh-dmatthews). Can be a follow-up for 1.53.
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.
Oh very cool, yes fine to document and officially support this!
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.
OK, I should make a PR with some tests before we document it @sfc-gh-dmatthews I will tag the PR with the feature label so you know it can be documented.
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.
question: Is this bottom padding in those snapshots just triggered by a different DOM element being used for screenshot? Or is there actually more padding.
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.
I guess this is the same as explained in the comments above? So no visible spacing changes on the actual app pages?
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.
Yeah, I looked through these and the differences reflect the bottom margins and similar which are cancelled by a negative margin on a parent and these were previously collapsed on this div and now are not. The markdown app is looking normal and the page snapshots are included because of the help tooltip change. If we were seeing changes in padding/margins they would also show up in the diffs for those pages.
lukasmasuch
left a comment
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.
LGTM 👍 a couple of comments
1f555f0 to
15a36b6
Compare
15a36b6 to
6fc8a70
Compare

Describe your changes
Adds a new parameter
text_alignmenttost.markdownandst.caption.Note: text is aligned via
text_alignment, tables and lists within the markdown will always be left-aligned.Try separating these into separate markdown blocks and using the flex alignment if you want to also align markdown tables and lists.
For example:
GitHub Issue Link (if applicable)
Closes #4109
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.