Spaces:
Sleeping
Sleeping
poemsforaphrodite
commited on
Commit
•
e0a861f
1
Parent(s):
2f0da4f
Update app.py
Browse files
app.py
CHANGED
@@ -475,38 +475,48 @@ def show_tabular_data(df, co):
|
|
475 |
start_idx = (st.session_state.current_page - 1) * rows_per_page
|
476 |
end_idx = start_idx + rows_per_page
|
477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
# Display column headers
|
479 |
-
cols = st.columns([3, 2, 1, 1, 1, 1, 1, 1])
|
480 |
-
headers = ['Page', 'Query', 'Clicks', 'Impressions', 'CTR', 'Position', 'Relevancy Score', 'Competitors']
|
481 |
for col, header in zip(cols, headers):
|
482 |
col.write(f"**{header}**")
|
483 |
|
|
|
|
|
|
|
|
|
484 |
# Display each row
|
485 |
for index, row in df.iloc[start_idx:end_idx].iterrows():
|
486 |
-
cols = st.columns([3, 2, 1, 1, 1, 1, 1, 1])
|
|
|
|
|
|
|
|
|
|
|
487 |
|
488 |
# Truncate and make the URL clickable
|
489 |
truncated_url = row['page'][:30] + '...' if len(row['page']) > 30 else row['page']
|
490 |
-
cols[
|
491 |
|
492 |
-
cols[
|
493 |
-
cols[
|
494 |
-
cols[
|
495 |
-
cols[
|
496 |
-
cols[
|
497 |
-
|
498 |
-
# Relevancy Score column
|
499 |
-
if pd.isna(row['relevancy_score']) or row['relevancy_score'] == 0:
|
500 |
-
if cols[6].button("Calculate", key=f"calc_{index}"):
|
501 |
-
with st.spinner('Calculating...'):
|
502 |
-
score = calculate_single_relevancy(row)
|
503 |
-
df.at[index, 'relevancy_score'] = score
|
504 |
-
st.experimental_rerun()
|
505 |
-
else:
|
506 |
-
cols[6].write(f"{row['relevancy_score']:.4f}")
|
507 |
|
508 |
# Competitors column
|
509 |
-
competitor_button = cols[
|
510 |
if competitor_button:
|
511 |
st.write(f"Competitor Analysis for: {row['query']}")
|
512 |
with st.spinner('Analyzing competitors...'):
|
|
|
475 |
start_idx = (st.session_state.current_page - 1) * rows_per_page
|
476 |
end_idx = start_idx + rows_per_page
|
477 |
|
478 |
+
# Add a "Calculate Relevancy" button at the top
|
479 |
+
if st.button("Calculate Relevancy for Selected"):
|
480 |
+
selected_indices = [i for i, selected in enumerate(st.session_state.selected_rows) if selected]
|
481 |
+
with st.spinner('Calculating relevancy scores...'):
|
482 |
+
for index in selected_indices:
|
483 |
+
if pd.isna(df.at[index, 'relevancy_score']) or df.at[index, 'relevancy_score'] == 0:
|
484 |
+
df.at[index, 'relevancy_score'] = calculate_single_relevancy(df.iloc[index])
|
485 |
+
st.success(f"Calculated relevancy scores for {len(selected_indices)} selected rows.")
|
486 |
+
st.experimental_rerun()
|
487 |
+
|
488 |
# Display column headers
|
489 |
+
cols = st.columns([0.5, 3, 2, 1, 1, 1, 1, 1, 1])
|
490 |
+
headers = ['Select', 'Page', 'Query', 'Clicks', 'Impressions', 'CTR', 'Position', 'Relevancy Score', 'Competitors']
|
491 |
for col, header in zip(cols, headers):
|
492 |
col.write(f"**{header}**")
|
493 |
|
494 |
+
# Initialize selected_rows in session state if not present
|
495 |
+
if 'selected_rows' not in st.session_state:
|
496 |
+
st.session_state.selected_rows = [False] * len(df)
|
497 |
+
|
498 |
# Display each row
|
499 |
for index, row in df.iloc[start_idx:end_idx].iterrows():
|
500 |
+
cols = st.columns([0.5, 3, 2, 1, 1, 1, 1, 1, 1])
|
501 |
+
|
502 |
+
# Checkbox for row selection
|
503 |
+
cols[0].checkbox("", key=f"select_{index}", value=st.session_state.selected_rows[index],
|
504 |
+
on_change=lambda i=index: setattr(st.session_state, 'selected_rows',
|
505 |
+
[True if j == i else x for j, x in enumerate(st.session_state.selected_rows)]))
|
506 |
|
507 |
# Truncate and make the URL clickable
|
508 |
truncated_url = row['page'][:30] + '...' if len(row['page']) > 30 else row['page']
|
509 |
+
cols[1].markdown(f"[{truncated_url}]({row['page']})")
|
510 |
|
511 |
+
cols[2].write(row['query'])
|
512 |
+
cols[3].write(row['clicks'])
|
513 |
+
cols[4].write(row['impressions'])
|
514 |
+
cols[5].write(f"{row['ctr']:.2%}")
|
515 |
+
cols[6].write(f"{row['position']:.1f}")
|
516 |
+
cols[7].write(f"{row['relevancy_score']:.4f}" if not pd.isna(row['relevancy_score']) and row['relevancy_score'] != 0 else "N/A")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
517 |
|
518 |
# Competitors column
|
519 |
+
competitor_button = cols[8].button("Show", key=f"comp_{index}", disabled=pd.isna(row['relevancy_score']) or row['relevancy_score'] == 0)
|
520 |
if competitor_button:
|
521 |
st.write(f"Competitor Analysis for: {row['query']}")
|
522 |
with st.spinner('Analyzing competitors...'):
|