poemsforaphrodite commited on
Commit
858a793
1 Parent(s): 6b4ee7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -484,8 +484,8 @@ def show_tabular_data(df, co):
484
  selected_indices = [i for i, selected in enumerate(st.session_state.selected_rows) if selected]
485
  with st.spinner('Calculating relevancy scores...'):
486
  for index in selected_indices:
487
- if pd.isna(df.at[index, 'relevancy_score']) or df.at[index, 'relevancy_score'] == 0:
488
- df.at[index, 'relevancy_score'] = calculate_single_relevancy(df.iloc[index])
489
  st.success(f"Calculated relevancy scores for {len(selected_indices)} selected rows.")
490
  st.experimental_rerun()
491
 
@@ -496,41 +496,41 @@ def show_tabular_data(df, co):
496
  col.write(f"**{header}**")
497
 
498
  # Display each row
499
- for i, (index, row) in enumerate(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...'):
523
- results_df = analyze_competitors(row, co)
524
 
525
  # Sort the results by relevancy score in descending order
526
  results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
527
 
528
  # Find our page's rank
529
- our_rank = results_df.index[results_df['url'] == row['page']].tolist()
530
  if our_rank:
531
  our_rank = our_rank[0] + 1 # Adding 1 because index starts at 0
532
  total_results = len(results_df)
533
- our_score = results_df.loc[results_df['url'] == row['page'], 'relevancy_score'].values[0]
534
 
535
  st.dataframe(results_df)
536
  st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
@@ -543,7 +543,7 @@ def show_tabular_data(df, co):
543
  elif our_rank > total_results / 2:
544
  st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
545
  else:
546
- st.error(f"Our page '{row['page']}' is not in the results. This indicates an error in fetching or processing the page.")
547
 
548
  return df # Return the updated dataframe
549
 
 
484
  selected_indices = [i for i, selected in enumerate(st.session_state.selected_rows) if selected]
485
  with st.spinner('Calculating relevancy scores...'):
486
  for index in selected_indices:
487
+ if pd.isna(df.iloc[index]['relevancy_score']) or df.iloc[index]['relevancy_score'] == 0:
488
+ df.iloc[index, df.columns.get_loc('relevancy_score')] = calculate_single_relevancy(df.iloc[index])
489
  st.success(f"Calculated relevancy scores for {len(selected_indices)} selected rows.")
490
  st.experimental_rerun()
491
 
 
496
  col.write(f"**{header}**")
497
 
498
  # Display each row
499
+ for i, row in enumerate(df.iloc[start_idx:end_idx].itertuples(), start=start_idx):
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_{i}", value=st.session_state.selected_rows[i],
504
+ on_change=lambda idx=i: setattr(st.session_state, 'selected_rows',
505
+ [True if j == idx 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_{i}", 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...'):
523
+ results_df = analyze_competitors(row._asdict(), co)
524
 
525
  # Sort the results by relevancy score in descending order
526
  results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
527
 
528
  # Find our page's rank
529
+ our_rank = results_df.index[results_df['url'] == row.page].tolist()
530
  if our_rank:
531
  our_rank = our_rank[0] + 1 # Adding 1 because index starts at 0
532
  total_results = len(results_df)
533
+ our_score = results_df.loc[results_df['url'] == row.page, 'relevancy_score'].values[0]
534
 
535
  st.dataframe(results_df)
536
  st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
 
543
  elif our_rank > total_results / 2:
544
  st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
545
  else:
546
+ st.error(f"Our page '{row.page}' is not in the results. This indicates an error in fetching or processing the page.")
547
 
548
  return df # Return the updated dataframe
549