"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 07 (tables)."""
import json
from playwright.sync_api import sync_playwright
BS5_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: system-ui, sans-serif; color: #212529; }
.table {
--bs-table-bg: transparent;
--bs-table-accent-bg: transparent;
--bs-table-striped-color: #212529;
--bs-table-striped-bg: rgba(0, 0, 0, 0.05);
--bs-table-active-color: #212529;
--bs-table-active-bg: rgba(0, 0, 0, 0.1);
--bs-table-hover-color: #212529;
--bs-table-hover-bg: rgba(0, 0, 0, 0.075);
width: 100%;
margin-bottom: 1rem;
color: #212529;
vertical-align: top;
border-color: #dee2e6;
border-collapse: collapse;
}
.table > :not(caption) > * > * {
padding: 0.5rem 0.5rem;
background-color: var(--bs-table-bg);
border-bottom: 1px solid var(--bs-table-border-color, #dee2e6);
box-shadow: inset 0 0 0 9999px var(--bs-table-accent-bg);
}
.table-sm > :not(caption) > * > * { padding: 0.25rem 0.25rem; }
.table-bordered > :not(caption) > * { border-width: 1px 0; border-style: solid; border-color: #dee2e6; }
.table-bordered > :not(caption) > * > * { border-width: 1px; border-style: solid; border-color: #dee2e6; }
.table-borderless > :not(caption) > * > * { border-bottom-width: 0; }
.table-striped > tbody > tr:nth-of-type(odd) > * { --bs-table-accent-bg: var(--bs-table-striped-bg); color: var(--bs-table-striped-color); }
.table-hover > tbody > tr:hover > * { --bs-table-accent-bg: var(--bs-table-hover-bg); color: var(--bs-table-hover-color); }
.table-primary { --bs-table-bg: #cfe2ff; --bs-table-border-color: #bacbe6; color: #000; }
.table-secondary { --bs-table-bg: #e2e3e5; --bs-table-border-color: #cbccce; color: #000; }
.table-success { --bs-table-bg: #d1e7dd; --bs-table-border-color: #bcd0c7; color: #000; }
.table-info { --bs-table-bg: #cff4fc; --bs-table-border-color: #badce3; color: #000; }
.table-warning { --bs-table-bg: #fff3cd; --bs-table-border-color: #e6dbb9; color: #000; }
.table-danger { --bs-table-bg: #f8d7da; --bs-table-border-color: #dfc2c4; color: #000; }
.table-light { --bs-table-bg: #f8f9fa; --bs-table-border-color: #dfe0e1; color: #000; }
.table-active { --bs-table-accent-bg: var(--bs-table-active-bg); color: var(--bs-table-active-color); }
.table-dark {
--bs-table-bg: #212529;
--bs-table-striped-bg: #2c3034;
--bs-table-striped-color: #fff;
--bs-table-hover-bg: #323539;
--bs-table-hover-color: #fff;
--bs-table-border-color: #373b3e;
color: #fff;
border-color: #373b3e;
}
.table-responsive { overflow-x: auto; }
@media (max-width: 575.98px) { .table-responsive-sm { overflow-x: auto; } }
@media (max-width: 767.98px) { .table-responsive-md { overflow-x: auto; } }
@media (max-width: 991.98px) { .table-responsive-lg { overflow-x: auto; } }
@media (max-width: 1199.98px) { .table-responsive-xl { overflow-x: auto; } }
@media (max-width: 1399.98px) { .table-responsive-xxl { overflow-x: auto; } }
"""
WIDE_COLS = "".join(f"<th style='min-width:120px'>Col{i}</th>" for i in range(1, 12))
WIDE_TDS = "".join("<td>Val</td>" for _ in range(1, 12))
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
<table id="t-basic" class="table"><tbody><tr><td>A</td></tr><tr><td>B</td></tr></tbody></table>
<table id="t-striped" class="table table-striped"><tbody><tr><td id="st-odd">Odd</td></tr><tr><td id="st-even">Even</td></tr></tbody></table>
<table id="t-bordered" class="table table-bordered"><tbody><tr><td id="bd-td">Bordered</td></tr></tbody></table>
<table id="t-borderless" class="table table-borderless"><tbody><tr><td id="bl-td">Borderless</td></tr></tbody></table>
<table id="t-hover" class="table table-hover"><tbody><tr id="hv-row"><td id="hv-td">HoverLight</td></tr></tbody></table>
<table id="t-dark-hover" class="table table-dark table-striped table-hover"><tbody><tr id="dhv-row"><td id="dhv-td">DarkRow1</td></tr><tr><td id="dhv-td2">DarkRow2</td></tr></tbody></table>
<table id="t-ctx" class="table">
<thead id="th-dark" class="table-dark"><tr><th id="th-dark-cell">HeadDark</th></tr></thead>
<tbody>
<tr class="table-primary"><td data-k="primary">P</td></tr>
<tr class="table-success"><td data-k="success">S</td></tr>
<tr class="table-danger"><td data-k="danger">D</td></tr>
<tr class="table-info"><td data-k="info">I</td></tr>
<tr class="table-warning"><td data-k="warning">W</td></tr>
<tr class="table-active"><td data-k="active">A</td></tr>
<tr class="table-secondary"><td data-k="secondary">Sec</td></tr>
<tr class="table-light"><td data-k="light">L</td></tr>
<tr class="table-dark"><td data-k="dark">Dk</td></tr>
</tbody>
</table>
<table class="table"><thead class="table-light"><tr><th id="th-light-cell">HeadLight</th></tr></thead></table>
<table id="t-sm" class="table table-bordered table-sm"><tbody><tr><td id="sm-td">Compact</td></tr></tbody></table>
<div id="resp-all" class="table-responsive"><table class="table"><thead><tr>{WIDE_COLS}</tr></thead><tbody><tr>{WIDE_TDS}</tr></tbody></table></div>
<div id="resp-sm" class="table-responsive-sm"><table class="table"><thead><tr>{WIDE_COLS}</tr></thead></table></div>
<div id="resp-md" class="table-responsive-md"><table class="table"><thead><tr>{WIDE_COLS}</tr></thead></table></div>
<div id="resp-lg" class="table-responsive-lg"><table class="table"><thead><tr>{WIDE_COLS}</tr></thead></table></div>
<div id="resp-xl" class="table-responsive-xl"><table class="table"><thead><tr>{WIDE_COLS}</tr></thead></table></div>
<div id="resp-xxl" class="table-responsive-xxl"><table class="table"><thead><tr>{WIDE_COLS}</tr></thead></table></div>
</body>
</html>
"""
def launch_browser(pw):
try:
return pw.chromium.launch(headless=True)
except Exception:
return pw.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1000, "height": 900})
page.set_content(HTML_DOC)
# Case 1: basic, striped, bordered, borderless
c1 = page.evaluate("""() => {
const tb = getComputedStyle(document.getElementById('t-basic'));
const btd = getComputedStyle(document.querySelector('#t-basic td'));
const odd = getComputedStyle(document.getElementById('st-odd'));
const even = getComputedStyle(document.getElementById('st-even'));
const bd = getComputedStyle(document.getElementById('bd-td'));
const bl = getComputedStyle(document.getElementById('bl-td'));
return {
border_collapse: tb.borderCollapse,
basic_padding: [btd.paddingTop, btd.paddingLeft],
basic_bottom_border: btd.borderBottomWidth,
striped_odd_differs_from_even: odd.boxShadow !== even.boxShadow,
bordered_left_width: bd.borderLeftWidth,
borderless_bottom_width: bl.borderBottomWidth
};
}""")
exp1 = {
"border_collapse": "collapse",
"basic_padding": ["8px", "8px"],
"basic_bottom_border": "1px",
"striped_odd_differs_from_even": True,
"bordered_left_width": "1px",
"borderless_bottom_width": "0px",
}
cases.append({
"name": "basic_striped_bordered_borderless",
"passed": c1 == exp1,
"expected": exp1,
"actual": c1,
})
# Case 2: hover & dark combinations (with real Playwright hover)
hv_before = page.eval_on_selector("#hv-td", "el => getComputedStyle(el).boxShadow")
page.hover("#hv-row")
hv_after = page.eval_on_selector("#hv-td", "el => getComputedStyle(el).boxShadow")
dk_bg = page.eval_on_selector("#dhv-td2", "el => getComputedStyle(el).backgroundColor")
dk_color = page.eval_on_selector("#dhv-td2", "el => getComputedStyle(el).color")
dhv_before = page.eval_on_selector("#dhv-td2", "el => getComputedStyle(el).boxShadow")
page.hover("#dhv-td2")
dhv_after = page.eval_on_selector("#dhv-td2", "el => getComputedStyle(el).boxShadow")
c2 = {
"light_hover_changes_accent": hv_before != hv_after,
"dark_bg": dk_bg,
"dark_color": dk_color,
"dark_hover_changes_accent": dhv_before != dhv_after,
}
exp2 = {
"light_hover_changes_accent": True,
"dark_bg": "rgb(33, 37, 41)",
"dark_color": "rgb(255, 255, 255)",
"dark_hover_changes_accent": True,
}
cases.append({
"name": "hover_and_dark_combinations",
"passed": c2 == exp2,
"expected": exp2,
"actual": c2,
})
# Case 3: contextual row colors & thead-dark / thead-light (BS5 .table-dark / .table-light)
c3 = page.evaluate("""() => {
const rows = {};
for (const td of document.querySelectorAll('#t-ctx tbody td')) {
const cs = getComputedStyle(td);
rows[td.dataset.k] = td.dataset.k === 'active' ? cs.boxShadow.includes('0.1') : cs.backgroundColor;
}
return {
rows,
thead_dark_bg: getComputedStyle(document.getElementById('th-dark-cell')).backgroundColor,
thead_light_bg: getComputedStyle(document.getElementById('th-light-cell')).backgroundColor
};
}""")
exp3 = {
"rows": {
"primary": "rgb(207, 226, 255)",
"success": "rgb(209, 231, 221)",
"danger": "rgb(248, 215, 218)",
"info": "rgb(207, 244, 252)",
"warning": "rgb(255, 243, 205)",
"active": True,
"secondary": "rgb(226, 227, 229)",
"light": "rgb(248, 249, 250)",
"dark": "rgb(33, 37, 41)",
},
"thead_dark_bg": "rgb(33, 37, 41)",
"thead_light_bg": "rgb(248, 249, 250)",
}
cases.append({
"name": "contextual_row_and_thead_colors",
"passed": c3 == exp3,
"expected": exp3,
"actual": c3,
})
# Case 4: compact .table-sm padding
c4 = page.evaluate("""() => {
const norm = getComputedStyle(document.querySelector('#t-basic td'));
const sm = getComputedStyle(document.getElementById('sm-td'));
return {
normal_padding: [norm.paddingTop, norm.paddingLeft],
sm_padding: [sm.paddingTop, sm.paddingLeft]
};
}""")
exp4 = {
"normal_padding": ["8px", "8px"],
"sm_padding": ["4px", "4px"],
}
cases.append({
"name": "compact_table_sm",
"passed": c4 == exp4,
"expected": exp4,
"actual": c4,
})
# Case 5: responsive table breakpoints
resp_check = {}
for vw in (500, 700, 900, 1100, 1300, 1450):
page.set_viewport_size({"width": vw, "height": 900})
resp_check[str(vw)] = page.evaluate("""() => {
const pick = id => getComputedStyle(document.getElementById(id)).overflowX;
const ra = document.getElementById('resp-all');
return {
all: pick('resp-all'),
all_scrolls: ra.scrollWidth > ra.clientWidth,
sm: pick('resp-sm'),
md: pick('resp-md'),
lg: pick('resp-lg'),
xl: pick('resp-xl'),
xxl: pick('resp-xxl')
};
}""")
exp5 = {
"500": {"all": "auto", "all_scrolls": True, "sm": "auto", "md": "auto", "lg": "auto", "xl": "auto", "xxl": "auto"},
"700": {"all": "auto", "all_scrolls": True, "sm": "visible", "md": "auto", "lg": "auto", "xl": "auto", "xxl": "auto"},
"900": {"all": "auto", "all_scrolls": True, "sm": "visible", "md": "visible", "lg": "auto", "xl": "auto", "xxl": "auto"},
"1100": {"all": "auto", "all_scrolls": True, "sm": "visible", "md": "visible", "lg": "visible", "xl": "auto", "xxl": "auto"},
"1300": {"all": "auto", "all_scrolls": True, "sm": "visible", "md": "visible", "lg": "visible", "xl": "visible", "xxl": "auto"},
"1450": {"all": "auto", "all_scrolls": False, "sm": "visible", "md": "visible", "lg": "visible", "xl": "visible", "xxl": "visible"},
}
cases.append({
"name": "responsive_table_breakpoints",
"passed": resp_check == exp5,
"expected": exp5,
"actual": resp_check,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap5-tables-lab/explore-tables.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tables.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a732.zip