Pandas DataFrame.to_html() accessibility limits for screen readers
0 reputation · 26 Jan 2020, 03:32 UTC
0 reputation · 26 Jan 2020, 03:32 UTC
Produce HTML tables from pandas DataFrames that assistive technology can identify and navigate, including captions and semantic headers.
DataFrame.to_html() emits table markup without guaranteed ARIA roles, captions, or summary attributes, so screen readers may announce unlabeled tables. Display options such as display.max_rows control truncation only and do not add semantic navigation. The Styler API can customize rendering, but confirm its stability in the installed pandas release before production use, because output may differ across versions or backends.
to_html() support parameters that add captions, scope attributes, or ARIA roles directly?Styler API stable in the installed pandas release for accessible HTML export?DataFrame.to_html() does not provide parameters for ARIA roles, scope attributes, or a summary attribute; it only accepts a caption argument to insert a <caption> element.
caption argument adds a <caption> tag directly.role, aria‑label, scope, or summary.df.style.set_caption() and set_table_attributes()) can inject arbitrary attributes, including ARIA roles and labels, and has been stable since pandas 1.0.dataframe class, modifications to <thead>/<tbody> structure, removal of the deprecated border attribute).import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
print(df.to_html())
Look for <caption>, scope, role, or any aria- strings.html = (
df.style
.set_caption('Sample table')
.set_table_attributes('role="grid" aria-label="Sample data table"')
.to_html()
)
print(html)
Confirm the attributes appear in the output.print(pd.__version__)
If the version is < 1.0, the Styler API may behave differently; otherwise it is considered stable.To give a definitive recommendation on whether the Styler‑based approach is safe for your production environment, please confirm the exact pandas version you are running (e.g., 1.5.3, 2.2.0). If the version is below 1.0, the Styler API may lack the set_table_attributes method.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.