Files
XH-202626/XH-202626_原型系统源码与部署手册/01_原型系统源码/utils/session_helper.py
T

116 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""
全局状态与企业选择同步工具
跨页面共享 `st.session_state["global_selected_stock_code"]`
确保在一个页面选择企业后,切换到任意页面均自动保持该企业联动
包含自动隐藏 Streamlit 右上角 Deploy 按钮与默认 Header 的全局 CSS 样式
"""
import streamlit as st
from collectors.financial_collector import get_all_companies
GLOBAL_COMPANY_KEY = "global_selected_stock_code"
def hide_streamlit_header_footer():
"""彻底隐藏 Streamlit 右上角的 Deploy 按钮、工具栏及页脚"""
st.markdown("""
<style>
/* 隐藏右上角 Deploy 按钮及 Header 菜单 */
header[data-testid="stHeader"] {
display: none !important;
visibility: hidden !important;
height: 0px !important;
}
[data-testid="stDeployButton"] {
display: none !important;
visibility: hidden !important;
}
#MainMenu {
display: none !important;
visibility: hidden !important;
}
footer {
display: none !important;
visibility: hidden !important;
}
</style>
""", unsafe_allow_html=True)
def get_global_company_code() -> str:
"""获取当前全局选中的企业股票代码"""
companies = get_all_companies()
if GLOBAL_COMPANY_KEY not in st.session_state or not st.session_state[GLOBAL_COMPANY_KEY]:
st.session_state[GLOBAL_COMPANY_KEY] = companies[0]["stock_code"] if companies else "688256"
return st.session_state[GLOBAL_COMPANY_KEY]
def set_global_company_code(code: str):
"""保存选中的企业股票代码到全局 SessionState"""
st.session_state[GLOBAL_COMPANY_KEY] = code
def render_company_selector(label: str = "🏢 选择待评估科创企业", key_suffix: str = "main"):
"""
渲染与全局 session_state 双向同步的企业选择下拉框
返回: selected_company_dict (选中的企业数据字典)
"""
hide_streamlit_header_footer()
companies = get_all_companies()
if not companies:
return None
# 构建带图标与领域的名称映射
company_options = {f"{c['short_name']} ({c['stock_code']}) - {c['sector']}": c["stock_code"] for c in companies}
labels_list = list(company_options.keys())
codes_list = [c["stock_code"] for c in companies]
current_code = get_global_company_code()
default_index = codes_list.index(current_code) if current_code in codes_list else 0
selected_label = st.selectbox(
label,
labels_list,
index=default_index,
key=f"company_selector_{key_suffix}"
)
new_code = company_options[selected_label]
if new_code != st.session_state.get(GLOBAL_COMPANY_KEY):
set_global_company_code(new_code)
# 返回选中的完整企业字典
for c in companies:
if c["stock_code"] == new_code:
return c
return companies[0]
def render_sidebar_global_company_selector():
"""在侧边栏渲染全局企业选择器与状态指示标签,并自动注入隐藏 Deploy 的 CSS"""
hide_streamlit_header_footer()
companies = get_all_companies()
if not companies:
return
company_options = {f"{c['short_name']} ({c['stock_code']})": c["stock_code"] for c in companies}
labels_list = list(company_options.keys())
codes_list = [c["stock_code"] for c in companies]
current_code = get_global_company_code()
default_index = codes_list.index(current_code) if current_code in codes_list else 0
st.markdown("### 🏢 全局联动评估目标")
selected_label = st.selectbox(
"当前联动目标企业:",
labels_list,
index=default_index,
key="global_sidebar_company_selector"
)
new_code = company_options[selected_label]
if new_code != st.session_state.get(GLOBAL_COMPANY_KEY):
set_global_company_code(new_code)