UCS2014 commited on
Commit
c83a766
·
verified ·
1 Parent(s): a266ee9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -73,35 +73,57 @@ def img_tag(path: Path, alt: str, cls: str) -> str:
73
  def inject_background(image_path: Path, opacity: float = 0.12) -> None:
74
  """
75
  Paint a page-wide background image with adjustable opacity.
76
- Uses a ::before layer attached to the Streamlit app container.
77
  """
78
  uri = data_uri(image_path)
79
  if not uri:
 
 
80
  return
81
- # Tip: tweak filter() to taste (e.g., blur or desaturate for readability)
82
  st.markdown(f"""
83
  <style>
84
  :root {{
85
  --bg-opacity: {max(0.0, min(opacity, 1.0))};
86
  }}
87
- /* Full-page fixed background behind all content */
88
- [data-testid="stAppViewContainer"]::before {{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  content: "";
90
- position: fixed;
91
  inset: 0;
92
- z-index: -1;
93
  background-image: url('{uri}');
94
  background-size: cover;
95
  background-position: center;
96
  background-repeat: no-repeat;
97
  opacity: var(--bg-opacity);
98
- filter: saturate(0.9) brightness(1.05); /* add blur(2px) if you want */
 
99
  }}
100
 
101
- /* Keep main container transparent to let the bg show through the gaps */
102
- [data-testid="stAppViewContainer"] > .main {{
103
- background: transparent !important;
104
- }}
105
  </style>
106
  """, unsafe_allow_html=True)
107
 
 
73
  def inject_background(image_path: Path, opacity: float = 0.12) -> None:
74
  """
75
  Paint a page-wide background image with adjustable opacity.
76
+ Works across Streamlit versions by targeting .stApp and the view container.
77
  """
78
  uri = data_uri(image_path)
79
  if not uri:
80
+ # Optional: small on-screen hint to confirm path issues
81
+ st.caption(f"Background image not found: {image_path}")
82
  return
83
+
84
  st.markdown(f"""
85
  <style>
86
  :root {{
87
  --bg-opacity: {max(0.0, min(opacity, 1.0))};
88
  }}
89
+
90
+ /* Make sure the app root can host a positioned overlay */
91
+ .stApp {{
92
+ position: relative !important;
93
+ background: transparent !important;
94
+ }}
95
+
96
+ /* Some Streamlit builds still use this container; keep it transparent */
97
+ [data-testid="stAppViewContainer"] {{
98
+ background: transparent !important;
99
+ }}
100
+
101
+ /* Main content area should not paint over the background */
102
+ [data-testid="stAppViewContainer"] > .main {{
103
+ background: transparent !important;
104
+ }}
105
+
106
+ html, body {{
107
+ background: transparent !important;
108
+ }}
109
+
110
+ /* Background overlay inside .stApp so it always sits behind content */
111
+ .stApp::before {{
112
  content: "";
113
+ position: fixed; /* fixed to viewport */
114
  inset: 0;
115
+ z-index: 0; /* below all content we'll raise to 1 */
116
  background-image: url('{uri}');
117
  background-size: cover;
118
  background-position: center;
119
  background-repeat: no-repeat;
120
  opacity: var(--bg-opacity);
121
+ filter: saturate(0.9) brightness(1.05); /* add blur(2px) if desired */
122
+ pointer-events: none; /* never intercept clicks */
123
  }}
124
 
125
+ /* Ensure all Streamlit content stacks above the bg */
126
+ .stApp > * {{ position: relative; z-index: 1; }}
 
 
127
  </style>
128
  """, unsafe_allow_html=True)
129