Skip to content

Commit

Permalink
Fix bold font in header showing less weight (streamlit#9395)
Browse files Browse the repository at this point in the history
## Describe your changes

### Context
It's been a longstanding
[issue](streamlit#4428) where
setting H1 to bold actually set the font less bold.

Example:
```
st.write("# Hello **World**")
# or st.title("Hello **World**)
```
would result in 

![image](https://github.com/user-attachments/assets/ff53a5f1-49de-41e2-b6a0-98ebea383e94)

This is because the [default font-weight of `h1` is
700](https://github.com/streamlit/streamlit/blob/develop/frontend/lib/src/theme/globalStyles.ts#L181)
and [default of `strong` or `b` is
600](https://github.com/streamlit/streamlit/blob/develop/frontend/lib/src/theme/globalStyles.ts#L313)

I consulted Jessi and we land on this expected behavior: 
**Setting text to bold in headers will NOT change its font-weight**

### This change
I proposed overriding the css font-weight for `b` & `strong` inside `h1`
to the default 700 and left font-weight for `h2+` as-is (they already
match the bolded text's weight)
Also added an e2e test for font weights of all headers.

## GitHub Issue Link (if applicable)
streamlit#4428

## Testing Plan

- Unit Tests (JS) ✅
- E2E Tests


---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
  • Loading branch information
sfc-gh-pchiu authored Sep 5, 2024
1 parent f686cb6 commit 15d60c9
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 6 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions e2e_playwright/st_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ def draw_header_test(join_output):

"---"

with st.container():
st.text("Headers with bold syntax")

strings = [
"# Bold **header1**",
"## Bold **header2**",
"### Bold **header3**",
"#### Bold **header4**",
"##### Bold **header5**",
"###### Bold **header6**",
]
for string in strings:
st.markdown(string)

"---"

st.latex(r"\LaTeX")

st.latex(
Expand Down
35 changes: 29 additions & 6 deletions e2e_playwright/st_markdown_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_different_markdown_elements_in_one_block_displayed(

markdown_elements = themed_app.get_by_test_id("stMarkdown")

expect(markdown_elements).to_have_count(52)
expect(markdown_elements).to_have_count(59)

# Snapshot one big markdown block containing a variety of elements to reduce number
# of snapshots
Expand Down Expand Up @@ -181,6 +181,29 @@ def test_match_snapshot_for_column_beside_widget(
assert_snapshot(container, name="st_markdown-headers_beside_widget")


def test_match_snapshot_for_headers_bold_text(
app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that the headers with bold markdown syntex is correct."""
container = _get_container_of_text(app, "Headers with bold syntax")
assert_snapshot(container, name="st_markdown-headers_bold_syntax")

# H1 defaults to extra bold
h1 = app.locator("h1#bold-header1")
expect(h1.locator("strong").first).to_have_css("font-weight", "700")

header_ids = [
"h2#bold-header2",
"h3#bold-header3",
"h4#bold-header4",
"h5#bold-header5",
"h6#bold-header6",
]
for header_id in header_ids:
header = app.locator(header_id)
expect(header.locator("strong").first).to_have_css("font-weight", "600")


def test_help_tooltip_works(app: Page):
"""Test that the help tooltip is displayed on hover."""
# Get the first element in the main view:
Expand All @@ -192,13 +215,13 @@ def test_help_tooltip_works(app: Page):

def test_latex_elements(themed_app: Page, assert_snapshot: ImageCompareFunction):
latex_elements = themed_app.get_by_test_id("stMarkdown")
assert_snapshot(latex_elements.nth(48), name="st_latex-latex")
expect(themed_app.get_by_test_id("stMarkdown").nth(48)).to_contain_text("LATE​X")
assert_snapshot(latex_elements.nth(55), name="st_latex-latex")
expect(themed_app.get_by_test_id("stMarkdown").nth(55)).to_contain_text("LATE​X")

assert_snapshot(latex_elements.nth(49), name="st_latex-formula")
assert_snapshot(latex_elements.nth(56), name="st_latex-formula")

expect(themed_app.get_by_test_id("stMarkdown").nth(50)).to_contain_text("a + b")
assert_snapshot(latex_elements.nth(50), name="st_latex-sympy")
expect(themed_app.get_by_test_id("stMarkdown").nth(57)).to_contain_text("a + b")
assert_snapshot(latex_elements.nth(57), name="st_latex-sympy")


def test_large_image_in_markdown(app: Page, assert_snapshot: ImageCompareFunction):
Expand Down
6 changes: 6 additions & 0 deletions frontend/lib/src/theme/globalStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ export const globalStyles = (theme: EmotionTheme): SerializedStyles => css`
font-weight: ${theme.fontWeights.bold};
}
// Override h1 font weight to default weight
h1 b,
h1 strong {
font-weight: ${theme.fontWeights.extrabold};
}
// Mark
mark {
Expand Down

0 comments on commit 15d60c9

Please sign in to comment.