poemsforaphrodite commited on
Commit
42a6c9c
1 Parent(s): a9c2ced

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -265,26 +265,33 @@ def analyze_competitors(row, co, country_code):
265
  our_content = fetch_content(our_url, query)
266
  our_score = calculate_relevance_score(our_content, query, co)
267
 
 
268
  if not any(r['is_our_url'] for r in results):
269
  results.append({
270
- 'Position': len(results) + 1,
271
  'URL': our_url,
272
  'Score': our_score,
273
  'is_our_url': True
274
  })
275
 
276
- # Sort results by position
277
- results = sorted(results, key=lambda x: x['Position'])
278
-
279
  # Create DataFrame
280
  results_df = pd.DataFrame(results)
281
- results_df['Position'] = results_df['Position'].astype(int)
 
 
 
 
 
 
282
 
283
  # Mark our URL
284
  results_df['URL'] = results_df.apply(
285
  lambda x: f"{x['URL']} (Our URL)" if x['is_our_url'] else x['URL'], axis=1
286
  )
287
 
 
 
 
288
  # Keep only the columns we want to display
289
  results_df = results_df[['Position', 'URL', 'Score']]
290
 
@@ -294,7 +301,7 @@ def show_competitor_analysis(row, co, country_code):
294
  if st.button("Check Competitors", key=f"comp_{row['page']}"):
295
  st.write(f"Competitor Analysis for: {row['query']}")
296
  with st.spinner('Analyzing competitors...'):
297
- results_df = analyze_competitors(row, co, country_code=country_code)
298
 
299
  # Display the Markdown table
300
  st.markdown(results_df.to_markdown(index=False), unsafe_allow_html=True)
@@ -664,7 +671,7 @@ def show_tabular_data(df, co, country_code):
664
  cols[4].write(row.impressions)
665
  cols[5].write(f"{row.ctr:.2%}")
666
  cols[6].write(f"{row.position:.1f}")
667
- cols[7].write(f"{row.relevancy_score:.4f}" if not pd.isna(row.relevancy_score) and row.relevancy_score != 0 else "N/A")
668
 
669
  # Competitors column
670
  if not pd.isna(row.relevancy_score) and row.relevancy_score != 0:
@@ -684,15 +691,12 @@ def show_tabular_data(df, co, country_code):
684
  with st.spinner('Analyzing competitors...'):
685
  results_df = analyze_competitors(row._asdict(), co, country_code=country_code)
686
 
687
- # Sort the results by Position in ascending order
688
  results_df = results_df.sort_values('Position', ascending=True).reset_index(drop=True)
689
 
690
- # Update the Position for our URL
691
  our_url_mask = results_df['URL'].str.contains('Our URL')
692
- results_df.loc[our_url_mask, 'Position'] = row.position
693
-
694
- # Ensure our URL's score matches the main table
695
- results_df.loc[our_url_mask, 'Score'] = row.relevancy_score
696
 
697
  # Create a custom style function to highlight only our URL's row
698
  def highlight_our_url(row):
 
265
  our_content = fetch_content(our_url, query)
266
  our_score = calculate_relevance_score(our_content, query, co)
267
 
268
+ # Add our URL to the results if it's not already there
269
  if not any(r['is_our_url'] for r in results):
270
  results.append({
271
+ 'Position': row['position'], # Use the position from the main table
272
  'URL': our_url,
273
  'Score': our_score,
274
  'is_our_url': True
275
  })
276
 
 
 
 
277
  # Create DataFrame
278
  results_df = pd.DataFrame(results)
279
+ results_df['Position'] = results_df['Position'].astype(float) # Convert to float for proper sorting
280
+
281
+ # Sort results by position
282
+ results_df = results_df.sort_values('Position', ascending=True).reset_index(drop=True)
283
+
284
+ # Update positions to be consecutive integers
285
+ results_df['Position'] = range(1, len(results_df) + 1)
286
 
287
  # Mark our URL
288
  results_df['URL'] = results_df.apply(
289
  lambda x: f"{x['URL']} (Our URL)" if x['is_our_url'] else x['URL'], axis=1
290
  )
291
 
292
+ # Format Score to 2 decimal points
293
+ results_df['Score'] = results_df['Score'].apply(lambda x: f"{x:.2f}")
294
+
295
  # Keep only the columns we want to display
296
  results_df = results_df[['Position', 'URL', 'Score']]
297
 
 
301
  if st.button("Check Competitors", key=f"comp_{row['page']}"):
302
  st.write(f"Competitor Analysis for: {row['query']}")
303
  with st.spinner('Analyzing competitors...'):
304
+ results_df = analyze_competitors(row, co, country_code)
305
 
306
  # Display the Markdown table
307
  st.markdown(results_df.to_markdown(index=False), unsafe_allow_html=True)
 
671
  cols[4].write(row.impressions)
672
  cols[5].write(f"{row.ctr:.2%}")
673
  cols[6].write(f"{row.position:.1f}")
674
+ cols[7].write(f"{row.relevancy_score:.2f}" if not pd.isna(row.relevancy_score) and row.relevancy_score != 0 else "N/A")
675
 
676
  # Competitors column
677
  if not pd.isna(row.relevancy_score) and row.relevancy_score != 0:
 
691
  with st.spinner('Analyzing competitors...'):
692
  results_df = analyze_competitors(row._asdict(), co, country_code=country_code)
693
 
694
+ # Sort the results by Position in ascending order (should already be sorted, but just in case)
695
  results_df = results_df.sort_values('Position', ascending=True).reset_index(drop=True)
696
 
697
+ # Ensure our URL's score matches the main table and format to 2 decimal points
698
  our_url_mask = results_df['URL'].str.contains('Our URL')
699
+ results_df.loc[our_url_mask, 'Score'] = f"{row.relevancy_score:.2f}"
 
 
 
700
 
701
  # Create a custom style function to highlight only our URL's row
702
  def highlight_our_url(row):