"""共享界面布局样式。""" import streamlit as st COMPACT_CSS = """ header[data-testid="stHeader"] { background: transparent; height: 2.25rem; } div[data-testid="stDecoration"] { display: none; } .block-container { padding-top: 0.85rem !important; padding-bottom: 1rem !important; padding-left: 1.75rem !important; padding-right: 1.75rem !important; max-width: 1200px; } /* 允许折叠按钮溢出到侧栏外侧 */ div[data-testid="stApp"], div[data-testid="stAppViewContainer"], section[data-testid="stSidebar"], section[data-testid="stSidebar"] > div, section[data-testid="stSidebar"] [data-testid="stSidebarContent"] { overflow: visible !important; } section[data-testid="stSidebar"] [data-testid="stSidebarUserContent"] { padding-top: 0.45rem !important; padding-bottom: 0.75rem !important; } /* 顶部 header 行高度清零,标题贴顶 */ section[data-testid="stSidebar"] [data-testid="stSidebarHeader"] { height: 0 !important; min-height: 0 !important; margin: 0 !important; padding: 0 !important; border: 0 !important; overflow: visible !important; position: relative !important; background: transparent !important; } section[data-testid="stSidebar"] [data-testid="stLogo"] { display: none !important; } /* 折叠按钮固定显示在侧栏右缘外侧 */ section[data-testid="stSidebar"] [data-testid="stSidebarCollapseButton"], section[data-testid="stSidebar"] [data-testid="stSidebarCollapseButton"] * { visibility: visible !important; opacity: 1 !important; } section[data-testid="stSidebar"] [data-testid="stSidebarCollapseButton"] { position: absolute !important; top: 0.45rem !important; right: 0 !important; transform: translateX(70%) !important; z-index: 1000000 !important; width: auto !important; margin: 0 !important; padding: 0 !important; pointer-events: auto !important; } section[data-testid="stSidebar"] [data-testid="stSidebarCollapseButton"] button { background: #FFFFFF !important; border: 1px solid #D5DEE8 !important; border-radius: 999px !important; box-shadow: 0 1px 3px rgba(22, 48, 71, 0.14) !important; width: 1.7rem !important; height: 1.7rem !important; min-height: 1.7rem !important; padding: 0 !important; } section[data-testid="stSidebar"] [data-testid="stSidebarCollapseButton"] span { font-size: 1.05rem !important; } /* 品牌与导航贴顶、间距收紧 */ section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h1, section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h2, section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h3 { margin: 0 0 0.15rem 0 !important; padding: 0 !important; line-height: 1.2 !important; } section[data-testid="stSidebar"] [data-testid="stCaptionContainer"] { margin-top: 0 !important; margin-bottom: 0.4rem !important; } section[data-testid="stSidebar"] a[data-testid="stPageLink-NavLink"] { padding-top: 0.3rem !important; padding-bottom: 0.3rem !important; margin: 0.02rem 0 !important; } div[data-testid="stVerticalBlock"] { gap: 0.5rem !important; } div[data-testid="stHorizontalBlock"] { gap: 0.7rem !important; } [data-testid="stMetric"] { background: #EDF2F7; border-radius: 0.55rem; padding: 0.5rem 0.75rem; } [data-testid="stMetricValue"] { font-size: 1.5rem !important; } h1 { font-size: 1.6rem !important; margin-bottom: 0.15rem !important; } h2, h3 { margin-top: 0.3rem !important; margin-bottom: 0.2rem !important; } hr { margin: 0.35rem 0 !important; } """ def apply_compact_layout(): # st.html 比 markdown 更稳定地注入样式,避免侧栏折叠按钮样式被忽略。 st.html(f"")