Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ def clean_movie_title(title):
|
|
| 18 |
|
| 19 |
def style_efficiency(row):
|
| 20 |
green = 'background-color: #E6F5E6;' # Light Green
|
| 21 |
-
red = 'background-color: #FFE5E5;'
|
| 22 |
default = ''
|
| 23 |
styles = [default] * len(row)
|
| 24 |
seat_efficiency = row.get('座次效率', 0)
|
|
@@ -161,41 +161,43 @@ def get_circled_number(hall_name):
|
|
| 161 |
def format_play_time(time_str):
|
| 162 |
if not time_str or not isinstance(time_str, str): return None
|
| 163 |
try:
|
| 164 |
-
parts = time_str.split(':');
|
| 165 |
-
hours = int(parts[0]);
|
| 166 |
-
minutes = int(parts[1])
|
| 167 |
return hours * 60 + minutes
|
| 168 |
except (ValueError, IndexError):
|
| 169 |
return None
|
| 170 |
|
| 171 |
|
| 172 |
-
# ---
|
| 173 |
def add_tms_locations_to_analysis(analysis_df, tms_movie_list):
|
| 174 |
locations = []
|
| 175 |
for index, row in analysis_df.iterrows():
|
| 176 |
movie_title = row['影片']
|
| 177 |
found_versions = []
|
| 178 |
for tms_movie in tms_movie_list:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
version_name = tms_movie['assert_name'].replace(movie_title, '').strip()
|
| 182 |
circled_halls = " ".join(sorted([get_circled_number(h) for h in tms_movie['halls']]))
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
locations.append('|'.join(found_versions))
|
| 186 |
|
| 187 |
analysis_df['影片所在影厅位置'] = locations
|
| 188 |
return analysis_df
|
| 189 |
|
| 190 |
-
|
| 191 |
# --- Streamlit Main UI ---
|
| 192 |
st.title('影城排片效率与内容分析工具')
|
| 193 |
st.write("上传 `影片映出日累计报表.xlsx` 进行效率分析,或点击下方按钮查询 TMS 服务器影片内容。")
|
| 194 |
|
| 195 |
uploaded_file = st.file_uploader("请在此处上传 Excel 文件", type=['xlsx', 'xls', 'csv'])
|
| 196 |
-
# NEW: Checkbox for the new feature
|
| 197 |
query_tms_for_location = st.checkbox("查询 TMS 找影片所在影厅")
|
| 198 |
|
|
|
|
| 199 |
if uploaded_file is not None:
|
| 200 |
try:
|
| 201 |
df = pd.read_excel(uploaded_file, skiprows=3, header=None)
|
|
@@ -214,17 +216,28 @@ if uploaded_file is not None:
|
|
| 214 |
'场次效率': '{:.2f}'}
|
| 215 |
|
| 216 |
full_day_analysis = process_and_analyze_data(df.copy())
|
| 217 |
-
prime_time_analysis = process_and_analyze_data(
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
# --- NEW LOGIC: If checkbox is ticked, fetch data and modify dataframes ---
|
| 221 |
if query_tms_for_location:
|
| 222 |
with st.spinner("正在关联查询 TMS 服务器..."):
|
| 223 |
_, tms_movie_list = fetch_and_process_server_movies()
|
| 224 |
full_day_analysis = add_tms_locations_to_analysis(full_day_analysis, tms_movie_list)
|
| 225 |
prime_time_analysis = add_tms_locations_to_analysis(prime_time_analysis, tms_movie_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
st.toast("TMS 影片位置关联成功!", icon="🔗")
|
| 227 |
|
|
|
|
| 228 |
st.markdown("### 全天排片效率分析")
|
| 229 |
if not full_day_analysis.empty:
|
| 230 |
st.dataframe(
|
|
@@ -246,6 +259,7 @@ if uploaded_file is not None:
|
|
| 246 |
except Exception as e:
|
| 247 |
st.error(f"处理文件时出错: {e}")
|
| 248 |
|
|
|
|
| 249 |
st.divider()
|
| 250 |
st.markdown("### TMS 服务器影片内容查询")
|
| 251 |
if st.button('点击查询 TMS 服务器'):
|
|
@@ -255,10 +269,7 @@ if st.button('点击查询 TMS 服务器'):
|
|
| 255 |
st.toast("TMS 服务器数据获取成功!", icon="🎉")
|
| 256 |
|
| 257 |
st.markdown("#### 按影片查看所在影厅")
|
| 258 |
-
view2_data = [{'影片名称': item['assert_name'],
|
| 259 |
-
'所在影厅': " ".join(sorted([get_circled_number(h) for h in item['halls']])),
|
| 260 |
-
'文件名': item['content_name'], '时长': format_play_time(item['play_time'])} for item in
|
| 261 |
-
movie_list_sorted]
|
| 262 |
df_view2 = pd.DataFrame(view2_data)
|
| 263 |
st.dataframe(df_view2, hide_index=True, use_container_width=True)
|
| 264 |
|
|
@@ -266,13 +277,10 @@ if st.button('点击查询 TMS 服务器'):
|
|
| 266 |
hall_tabs = st.tabs(halls_data.keys())
|
| 267 |
for tab, hall_name in zip(hall_tabs, halls_data.keys()):
|
| 268 |
with tab:
|
| 269 |
-
view1_data_for_tab = [{'影片名称': item['details']['assert_name'], '所在影厅': " ".join(
|
| 270 |
-
sorted([get_circled_number(h) for h in item['details']['halls']])),
|
| 271 |
-
'文件名': item['content_name'],
|
| 272 |
-
'时长': format_play_time(item['details']['play_time'])} for item in
|
| 273 |
-
halls_data[hall_name]]
|
| 274 |
df_view1_tab = pd.DataFrame(view1_data_for_tab)
|
| 275 |
st.dataframe(df_view1_tab, hide_index=True, use_container_width=True)
|
| 276 |
|
| 277 |
except Exception as e:
|
| 278 |
-
st.error(f"查询服务器时出错: {e}")
|
|
|
|
|
|
| 18 |
|
| 19 |
def style_efficiency(row):
|
| 20 |
green = 'background-color: #E6F5E6;' # Light Green
|
| 21 |
+
red = 'background-color: #FFE5E5;' # Light Red
|
| 22 |
default = ''
|
| 23 |
styles = [default] * len(row)
|
| 24 |
seat_efficiency = row.get('座次效率', 0)
|
|
|
|
| 161 |
def format_play_time(time_str):
|
| 162 |
if not time_str or not isinstance(time_str, str): return None
|
| 163 |
try:
|
| 164 |
+
parts = time_str.split(':'); hours = int(parts[0]); minutes = int(parts[1])
|
|
|
|
|
|
|
| 165 |
return hours * 60 + minutes
|
| 166 |
except (ValueError, IndexError):
|
| 167 |
return None
|
| 168 |
|
| 169 |
|
| 170 |
+
# --- UPDATED Helper function to add TMS location column ---
|
| 171 |
def add_tms_locations_to_analysis(analysis_df, tms_movie_list):
|
| 172 |
locations = []
|
| 173 |
for index, row in analysis_df.iterrows():
|
| 174 |
movie_title = row['影片']
|
| 175 |
found_versions = []
|
| 176 |
for tms_movie in tms_movie_list:
|
| 177 |
+
# FIX 3: Change matching from 'in' to 'startswith'
|
| 178 |
+
if tms_movie['assert_name'].startswith(movie_title):
|
| 179 |
version_name = tms_movie['assert_name'].replace(movie_title, '').strip()
|
| 180 |
circled_halls = " ".join(sorted([get_circled_number(h) for h in tms_movie['halls']]))
|
| 181 |
+
|
| 182 |
+
# FIX 2: Handle empty version name to remove colon
|
| 183 |
+
if version_name:
|
| 184 |
+
found_versions.append(f"{version_name}:{circled_halls}")
|
| 185 |
+
else:
|
| 186 |
+
found_versions.append(circled_halls)
|
| 187 |
+
|
| 188 |
locations.append('|'.join(found_versions))
|
| 189 |
|
| 190 |
analysis_df['影片所在影厅位置'] = locations
|
| 191 |
return analysis_df
|
| 192 |
|
|
|
|
| 193 |
# --- Streamlit Main UI ---
|
| 194 |
st.title('影城排片效率与内容分析工具')
|
| 195 |
st.write("上传 `影片映出日累计报表.xlsx` 进行效率分析,或点击下方按钮查询 TMS 服务器影片内容。")
|
| 196 |
|
| 197 |
uploaded_file = st.file_uploader("请在此处上传 Excel 文件", type=['xlsx', 'xls', 'csv'])
|
|
|
|
| 198 |
query_tms_for_location = st.checkbox("查询 TMS 找影片所在影厅")
|
| 199 |
|
| 200 |
+
|
| 201 |
if uploaded_file is not None:
|
| 202 |
try:
|
| 203 |
df = pd.read_excel(uploaded_file, skiprows=3, header=None)
|
|
|
|
| 216 |
'场次效率': '{:.2f}'}
|
| 217 |
|
| 218 |
full_day_analysis = process_and_analyze_data(df.copy())
|
| 219 |
+
prime_time_analysis = process_and_analyze_data(df[df['放映时间'].between(pd.to_datetime('14:00:00').time(), pd.to_datetime('21:00:00').time())].copy())
|
| 220 |
+
|
|
|
|
|
|
|
| 221 |
if query_tms_for_location:
|
| 222 |
with st.spinner("正在关联查询 TMS 服务器..."):
|
| 223 |
_, tms_movie_list = fetch_and_process_server_movies()
|
| 224 |
full_day_analysis = add_tms_locations_to_analysis(full_day_analysis, tms_movie_list)
|
| 225 |
prime_time_analysis = add_tms_locations_to_analysis(prime_time_analysis, tms_movie_list)
|
| 226 |
+
|
| 227 |
+
# FIX 1: Reorder columns
|
| 228 |
+
if '影片所在影厅位置' in full_day_analysis.columns:
|
| 229 |
+
cols_full = full_day_analysis.columns.tolist()
|
| 230 |
+
cols_full.insert(1, cols_full.pop(cols_full.index('影片所在影厅位置')))
|
| 231 |
+
full_day_analysis = full_day_analysis[cols_full]
|
| 232 |
+
|
| 233 |
+
if '影片所在影厅位置' in prime_time_analysis.columns:
|
| 234 |
+
cols_prime = prime_time_analysis.columns.tolist()
|
| 235 |
+
cols_prime.insert(1, cols_prime.pop(cols_prime.index('影片所在影厅位置')))
|
| 236 |
+
prime_time_analysis = prime_time_analysis[cols_prime]
|
| 237 |
+
|
| 238 |
st.toast("TMS 影片位置关联成功!", icon="🔗")
|
| 239 |
|
| 240 |
+
|
| 241 |
st.markdown("### 全天排片效率分析")
|
| 242 |
if not full_day_analysis.empty:
|
| 243 |
st.dataframe(
|
|
|
|
| 259 |
except Exception as e:
|
| 260 |
st.error(f"处理文件时出错: {e}")
|
| 261 |
|
| 262 |
+
|
| 263 |
st.divider()
|
| 264 |
st.markdown("### TMS 服务器影片内容查询")
|
| 265 |
if st.button('点击查询 TMS 服务器'):
|
|
|
|
| 269 |
st.toast("TMS 服务器数据获取成功!", icon="🎉")
|
| 270 |
|
| 271 |
st.markdown("#### 按影片查看所在影厅")
|
| 272 |
+
view2_data = [{'影片名称': item['assert_name'], '所在影厅': " ".join(sorted([get_circled_number(h) for h in item['halls']])), '文件名': item['content_name'], '时长': format_play_time(item['play_time'])} for item in movie_list_sorted]
|
|
|
|
|
|
|
|
|
|
| 273 |
df_view2 = pd.DataFrame(view2_data)
|
| 274 |
st.dataframe(df_view2, hide_index=True, use_container_width=True)
|
| 275 |
|
|
|
|
| 277 |
hall_tabs = st.tabs(halls_data.keys())
|
| 278 |
for tab, hall_name in zip(hall_tabs, halls_data.keys()):
|
| 279 |
with tab:
|
| 280 |
+
view1_data_for_tab = [{'影片名称': item['details']['assert_name'], '所在影厅': " ".join(sorted([get_circled_number(h) for h in item['details']['halls']])), '文件名': item['content_name'], '时长': format_play_time(item['details']['play_time'])} for item in halls_data[hall_name]]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
df_view1_tab = pd.DataFrame(view1_data_for_tab)
|
| 282 |
st.dataframe(df_view1_tab, hide_index=True, use_container_width=True)
|
| 283 |
|
| 284 |
except Exception as e:
|
| 285 |
+
st.error(f"查询服务器时出错: {e}")
|
| 286 |
+
|