poemsforaphrodite commited on
Commit
9b49b5b
1 Parent(s): 437da00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -46
app.py CHANGED
@@ -475,52 +475,61 @@ 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
- # Prepare data for display
479
- display_df = df.iloc[start_idx:end_idx].copy()
480
- display_df['page'] = display_df['page'].apply(lambda x: x[:50] + '...' if len(x) > 50 else x)
481
- display_df['ctr'] = display_df['ctr'].apply(lambda x: f"{x:.2%}")
482
- display_df['position'] = display_df['position'].apply(lambda x: f"{x:.1f}")
483
- display_df['relevancy_score'] = display_df['relevancy_score'].apply(lambda x: f"{x:.4f}" if pd.notnull(x) else "N/A")
484
-
485
- # Display the table
486
- st.table(display_df)
487
-
488
- # Add calculate and show competitors buttons
489
- for index, row in display_df.iterrows():
490
- col1, col2 = st.columns(2)
491
- with col1:
492
- if pd.isna(row['relevancy_score']) or row['relevancy_score'] == "N/A":
493
- if st.button("Calculate", key=f"calc_{index}"):
494
- with st.spinner('Calculating...'):
495
- score = calculate_single_relevancy(df.loc[index])
496
- df.at[index, 'relevancy_score'] = score
497
- st.experimental_rerun()
498
- with col2:
499
- if row['relevancy_score'] != "N/A":
500
- if st.button("Show Competitors", key=f"comp_{index}"):
501
- st.write(f"Competitor Analysis for: {row['query']}")
502
- with st.spinner('Analyzing competitors...'):
503
- results_df = analyze_competitors(df.loc[index], co)
504
- results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
505
-
506
- our_rank = results_df.index[results_df['url'] == row['page']].tolist()
507
- if our_rank:
508
- our_rank = our_rank[0] + 1
509
- total_results = len(results_df)
510
- our_score = results_df.loc[results_df['url'] == row['page'], 'relevancy_score'].values[0]
511
-
512
- st.table(results_df)
513
- st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
514
- st.write(f"Our relevancy score: {our_score:.4f}")
515
-
516
- if our_rank == 1:
517
- st.success("Your page has the highest relevancy score!")
518
- elif our_rank <= 3:
519
- st.info("Your page is among the top 3 most relevant results.")
520
- elif our_rank > total_results / 2:
521
- st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
522
- else:
523
- st.error(f"Our page '{row['page']}' is not in the results. This indicates an error in fetching or processing the page.")
 
 
 
 
 
 
 
 
 
524
 
525
  return df # Return the updated dataframe
526
 
 
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
+ cols[0].write(row['page'])
488
+ cols[1].write(row['query'])
489
+ cols[2].write(row['clicks'])
490
+ cols[3].write(row['impressions'])
491
+ cols[4].write(f"{row['ctr']:.2%}")
492
+ cols[5].write(f"{row['position']:.1f}")
493
+
494
+ # Relevancy Score column
495
+ if pd.isna(row['relevancy_score']) or row['relevancy_score'] == 0:
496
+ if cols[6].button("Calculate", key=f"calc_{index}"):
497
+ with st.spinner('Calculating...'):
498
+ score = calculate_single_relevancy(row)
499
+ df.at[index, 'relevancy_score'] = score
500
+ st.experimental_rerun()
501
+ else:
502
+ cols[6].write(f"{row['relevancy_score']:.4f}")
503
+
504
+ # Competitors column
505
+ competitor_button = cols[7].button("Show Competitors", key=f"comp_{index}", disabled=pd.isna(row['relevancy_score']) or row['relevancy_score'] == 0)
506
+ if competitor_button:
507
+ st.write(f"Competitor Analysis for: {row['query']}")
508
+ with st.spinner('Analyzing competitors...'):
509
+ results_df = analyze_competitors(row, co)
510
+
511
+ # Sort the results by relevancy score in descending order
512
+ results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
513
+
514
+ # Find our page's rank
515
+ our_rank = results_df.index[results_df['url'] == row['page']].tolist()
516
+ if our_rank:
517
+ our_rank = our_rank[0] + 1 # Adding 1 because index starts at 0
518
+ total_results = len(results_df)
519
+ our_score = results_df.loc[results_df['url'] == row['page'], 'relevancy_score'].values[0]
520
+
521
+ st.dataframe(results_df)
522
+ st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
523
+ st.write(f"Our relevancy score: {our_score:.4f}")
524
+
525
+ if our_rank == 1:
526
+ st.success("Your page has the highest relevancy score!")
527
+ elif our_rank <= 3:
528
+ st.info("Your page is among the top 3 most relevant results.")
529
+ elif our_rank > total_results / 2:
530
+ st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
531
+ else:
532
+ st.error(f"Our page '{row['page']}' is not in the results. This indicates an error in fetching or processing the page.")
533
 
534
  return df # Return the updated dataframe
535