sirochild commited on
Commit
b258751
·
verified ·
1 Parent(s): 326718c

Upload main_app.py

Browse files
Files changed (1) hide show
  1. main_app.py +90 -16
main_app.py CHANGED
@@ -1032,8 +1032,12 @@ def update_background(scene_manager: SceneManager, theme: str):
1032
  # デバッグ用:使用する背景画像URLをログ出力
1033
  selected_bg = css_dict.get(theme, css_dict['default'])
1034
  logger.info(f"背景画像設定: {selected_bg}")
 
 
 
 
1035
  css = f"""
1036
- <style>
1037
  /* 背景画像を確実に適用するための強力なセレクタ - 優先度を最大化 */
1038
  .stApp.stApp.stApp {{
1039
  {selected_bg}
@@ -1055,9 +1059,22 @@ def update_background(scene_manager: SceneManager, theme: str):
1055
  min-height: 100vh !important;
1056
  }}
1057
 
1058
- /* bodyとhtmlは透過に */
1059
- html, body {{
1060
- background: none !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
1061
  }}
1062
 
1063
  /* 他の要素は透過にして背景を見せる */
@@ -1067,6 +1084,47 @@ def update_background(scene_manager: SceneManager, theme: str):
1067
  background: transparent !important;
1068
  }}
1069
  </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1070
  """
1071
  st.markdown(css, unsafe_allow_html=True)
1072
  # Python側の状態管理は継続する
@@ -2368,6 +2426,8 @@ Streamlit情報:
2368
  if st.session_state.get('dog_button_clicked', False):
2369
  # フラグをクリアして重複処理を防ぐ
2370
  st.session_state.dog_button_clicked = False
 
 
2371
  logger.info("犬のボタン状態変更を検知しました - UI更新を実行")
2372
  # 状態変更が反映されるよう強制的に再描画
2373
  st.rerun()
@@ -3001,22 +3061,36 @@ def main():
3001
  # CSSを適用
3002
  inject_custom_css()
3003
 
3004
- # CSS読み込み完了後に背景を更新(初期設定・初回のみ)
3005
- if not st.session_state.get('_initial_background_set', False):
3006
- try:
3007
- initial_theme = st.session_state.chat['scene_params'].get('theme', 'default')
3008
- logger.info(f"初期背景設定: テーマ '{initial_theme}' (初回のみ)")
 
 
 
 
 
3009
 
3010
- # 初回のみ背景を更新
3011
- update_background(managers['scene_manager'], initial_theme)
3012
  st.session_state._initial_background_set = True
 
3013
  st.session_state._last_rendered_tab = None # タブ切り替え検出をリセット
 
 
 
 
 
 
 
 
3014
 
3015
- except Exception as e:
3016
- logger.error(f"初期背景設定でエラーが発生: {e}")
3017
- import traceback
3018
- logger.error(f"初期背景設定エラーの詳細: {traceback.format_exc()}")
3019
- # エラーが発生してもアプリケーションは継続
3020
 
3021
  # チュートリアル機能の初期化
3022
  tutorial_manager = managers['tutorial_manager']
 
1032
  # デバッグ用:使用する背景画像URLをログ出力
1033
  selected_bg = css_dict.get(theme, css_dict['default'])
1034
  logger.info(f"背景画像設定: {selected_bg}")
1035
+ # 一意なIDを生成してCSSの重複を防ぐ
1036
+ import time
1037
+ css_id = f"bg-{int(time.time() * 1000)}"
1038
+
1039
  css = f"""
1040
+ <style id="{css_id}">
1041
  /* 背景画像を確実に適用するための強力なセレクタ - 優先度を最大化 */
1042
  .stApp.stApp.stApp {{
1043
  {selected_bg}
 
1059
  min-height: 100vh !important;
1060
  }}
1061
 
1062
+ /* bodyとhtmlレベルでも背景を設定(rerun対策) */
1063
+ html {{
1064
+ {selected_bg}
1065
+ background-size: cover !important;
1066
+ background-position: center !important;
1067
+ background-attachment: fixed !important;
1068
+ background-repeat: no-repeat !important;
1069
+ }}
1070
+
1071
+ body {{
1072
+ {selected_bg}
1073
+ background-size: cover !important;
1074
+ background-position: center !important;
1075
+ background-attachment: fixed !important;
1076
+ background-repeat: no-repeat !important;
1077
+ min-height: 100vh !important;
1078
  }}
1079
 
1080
  /* 他の要素は透過にして背景を見せる */
 
1084
  background: transparent !important;
1085
  }}
1086
  </style>
1087
+
1088
+ <script>
1089
+ // JavaScript側でも背景を強制適用(rerun対策)
1090
+ (function() {{
1091
+ const applyBackground = () => {{
1092
+ const elements = [
1093
+ document.documentElement,
1094
+ document.body,
1095
+ document.querySelector('.stApp'),
1096
+ document.querySelector('[data-testid="stAppViewContainer"]')
1097
+ ];
1098
+
1099
+ elements.forEach(el => {{
1100
+ if (el) {{
1101
+ el.style.backgroundImage = '{selected_bg.split(": ")[1].replace(";", "")}';
1102
+ el.style.backgroundSize = 'cover';
1103
+ el.style.backgroundPosition = 'center';
1104
+ el.style.backgroundAttachment = 'fixed';
1105
+ el.style.backgroundRepeat = 'no-repeat';
1106
+ el.style.minHeight = '100vh';
1107
+ }}
1108
+ }});
1109
+ }};
1110
+
1111
+ // 即座に適用
1112
+ applyBackground();
1113
+
1114
+ // DOM変更時にも再適用
1115
+ if (window.MutationObserver) {{
1116
+ const observer = new MutationObserver(applyBackground);
1117
+ observer.observe(document.body, {{ childList: true, subtree: true }});
1118
+
1119
+ // 5秒後に監視を停止(パフォーマンス対策)
1120
+ setTimeout(() => observer.disconnect(), 5000);
1121
+ }}
1122
+
1123
+ // 定期的にも適用(最後の手段)
1124
+ setTimeout(applyBackground, 100);
1125
+ setTimeout(applyBackground, 500);
1126
+ }})();
1127
+ </script>
1128
  """
1129
  st.markdown(css, unsafe_allow_html=True)
1130
  # Python側の状態管理は継続する
 
2426
  if st.session_state.get('dog_button_clicked', False):
2427
  # フラグをクリアして重複処理を防ぐ
2428
  st.session_state.dog_button_clicked = False
2429
+ # 背景更新を強制
2430
+ st.session_state.force_background_update = True
2431
  logger.info("犬のボタン状態変更を検知しました - UI更新を実行")
2432
  # 状態変更が反映されるよう強制的に再描画
2433
  st.rerun()
 
3061
  # CSSを適用
3062
  inject_custom_css()
3063
 
3064
+ # 背景を毎回更新(rerun時の白背景問題を解決)
3065
+ try:
3066
+ current_theme = st.session_state.chat['scene_params'].get('theme', 'default')
3067
+
3068
+ # 背景更新が必要かチェック
3069
+ last_theme = st.session_state.get('_last_background_theme', None)
3070
+ force_background_update = st.session_state.get('force_background_update', False)
3071
+
3072
+ if last_theme != current_theme or force_background_update or not st.session_state.get('_initial_background_set', False):
3073
+ logger.info(f"背景更新: テーマ '{current_theme}' (前回: {last_theme})")
3074
 
3075
+ # 背景を更新
3076
+ update_background(managers['scene_manager'], current_theme)
3077
  st.session_state._initial_background_set = True
3078
+ st.session_state._last_background_theme = current_theme
3079
  st.session_state._last_rendered_tab = None # タブ切り替え検出をリセット
3080
+
3081
+ # 強制更新フラグをクリア
3082
+ if force_background_update:
3083
+ st.session_state.force_background_update = False
3084
+ else:
3085
+ # テーマが変わっていない場合でも、rerun時は背景を再適用
3086
+ logger.debug(f"背景再適用: テーマ '{current_theme}'")
3087
+ update_background(managers['scene_manager'], current_theme)
3088
 
3089
+ except Exception as e:
3090
+ logger.error(f"背景設定でエラーが発生: {e}")
3091
+ import traceback
3092
+ logger.error(f"背景設定エラーの詳細: {traceback.format_exc()}")
3093
+ # エラーが発生してもアプリケーションは継続
3094
 
3095
  # チュートリアル機能の初期化
3096
  tutorial_manager = managers['tutorial_manager']