style: 侧栏折叠按钮移到外侧并贴顶显示品牌导航
This commit is contained in:
+82
-28
@@ -2,73 +2,127 @@
|
|||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
|
||||||
COMPACT_CSS = """
|
COMPACT_CSS = """
|
||||||
<style>
|
|
||||||
header[data-testid="stHeader"] {
|
header[data-testid="stHeader"] {
|
||||||
background: transparent;
|
background: transparent;
|
||||||
height: 2.5rem;
|
height: 2.25rem;
|
||||||
}
|
}
|
||||||
div[data-testid="stDecoration"] {
|
div[data-testid="stDecoration"] {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
.block-container {
|
.block-container {
|
||||||
padding-top: 1.1rem !important;
|
padding-top: 0.85rem !important;
|
||||||
padding-bottom: 1.2rem !important;
|
padding-bottom: 1rem !important;
|
||||||
padding-left: 2rem !important;
|
padding-left: 1.75rem !important;
|
||||||
padding-right: 2rem !important;
|
padding-right: 1.75rem !important;
|
||||||
max-width: 1200px;
|
max-width: 1200px;
|
||||||
}
|
}
|
||||||
section[data-testid="stSidebar"] > div:first-child {
|
|
||||||
padding-top: 0.75rem !important;
|
/* 允许折叠按钮溢出到侧栏外侧 */
|
||||||
|
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;
|
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"] h1,
|
||||||
section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h2,
|
section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h2,
|
||||||
section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h3 {
|
section[data-testid="stSidebar"] [data-testid="stMarkdownContainer"] h3 {
|
||||||
margin: 0.15rem 0 0.2rem 0 !important;
|
margin: 0 0 0.15rem 0 !important;
|
||||||
padding: 0 !important;
|
padding: 0 !important;
|
||||||
line-height: 1.2 !important;
|
line-height: 1.2 !important;
|
||||||
}
|
}
|
||||||
section[data-testid="stSidebar"] [data-testid="stCaptionContainer"] {
|
section[data-testid="stSidebar"] [data-testid="stCaptionContainer"] {
|
||||||
margin-top: 0 !important;
|
margin-top: 0 !important;
|
||||||
margin-bottom: 0.55rem !important;
|
margin-bottom: 0.4rem !important;
|
||||||
}
|
}
|
||||||
section[data-testid="stSidebar"] [data-testid="stSidebarNavLink"],
|
|
||||||
section[data-testid="stSidebar"] a[data-testid="stPageLink-NavLink"] {
|
section[data-testid="stSidebar"] a[data-testid="stPageLink-NavLink"] {
|
||||||
padding-top: 0.35rem !important;
|
padding-top: 0.3rem !important;
|
||||||
padding-bottom: 0.35rem !important;
|
padding-bottom: 0.3rem !important;
|
||||||
margin: 0.05rem 0 !important;
|
margin: 0.02rem 0 !important;
|
||||||
}
|
|
||||||
div[data-testid="stMainBlockContainer"] > div {
|
|
||||||
gap: 0.65rem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
div[data-testid="stVerticalBlock"] {
|
div[data-testid="stVerticalBlock"] {
|
||||||
gap: 0.55rem !important;
|
gap: 0.5rem !important;
|
||||||
}
|
}
|
||||||
div[data-testid="stHorizontalBlock"] {
|
div[data-testid="stHorizontalBlock"] {
|
||||||
gap: 0.75rem !important;
|
gap: 0.7rem !important;
|
||||||
}
|
}
|
||||||
[data-testid="stMetric"] {
|
[data-testid="stMetric"] {
|
||||||
background: #EDF2F7;
|
background: #EDF2F7;
|
||||||
border-radius: 0.55rem;
|
border-radius: 0.55rem;
|
||||||
padding: 0.55rem 0.8rem;
|
padding: 0.5rem 0.75rem;
|
||||||
}
|
}
|
||||||
[data-testid="stMetricValue"] {
|
[data-testid="stMetricValue"] {
|
||||||
font-size: 1.55rem !important;
|
font-size: 1.5rem !important;
|
||||||
}
|
}
|
||||||
h1 {
|
h1 {
|
||||||
font-size: 1.7rem !important;
|
font-size: 1.6rem !important;
|
||||||
margin-bottom: 0.2rem !important;
|
margin-bottom: 0.15rem !important;
|
||||||
}
|
}
|
||||||
h2, h3 {
|
h2, h3 {
|
||||||
margin-top: 0.35rem !important;
|
margin-top: 0.3rem !important;
|
||||||
margin-bottom: 0.25rem !important;
|
margin-bottom: 0.2rem !important;
|
||||||
}
|
}
|
||||||
hr {
|
hr {
|
||||||
margin: 0.4rem 0 !important;
|
margin: 0.35rem 0 !important;
|
||||||
}
|
}
|
||||||
</style>
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def apply_compact_layout():
|
def apply_compact_layout():
|
||||||
st.markdown(COMPACT_CSS, unsafe_allow_html=True)
|
# st.html 比 markdown 更稳定地注入样式,避免侧栏折叠按钮样式被忽略。
|
||||||
|
st.html(f"<style>{COMPACT_CSS}</style>")
|
||||||
|
|||||||
Reference in New Issue
Block a user