poemsforaphrodite commited on
Commit
f20b94b
1 Parent(s): 295ba01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -254,7 +254,17 @@ def analyze_competitors(row, co, custom_url=None, country_code=None):
254
  our_position = next((r['position'] for r in results if r['url'] == our_url), len(results) + 1)
255
  results.append({'position': our_position, 'url': our_url, 'relevancy_score': our_score})
256
 
257
- results_df = pd.DataFrame(results).sort_values('position')
 
 
 
 
 
 
 
 
 
 
258
 
259
  return results_df
260
 
@@ -264,32 +274,36 @@ def show_competitor_analysis(row, co, country_code):
264
  with st.spinner('Analyzing competitors...'):
265
  results_df = analyze_competitors(row, co, country_code=country_code)
266
 
267
- # Remove duplicates, keeping the first occurrence
268
- results_df = results_df.drop_duplicates(subset='url', keep='first')
269
-
270
- # Format our result in bold
271
- results_df['URL'] = results_df.apply(
272
- lambda x: f"**{x['url']}**" if x['url'] == row['page'] else x['url'], axis=1
273
- )
274
 
275
- # Display the results
276
- st.write("Relevancy Score Comparison:")
277
- st.markdown(results_df[['position', 'URL', 'relevancy_score']].to_markdown(index=False), unsafe_allow_html=True)
278
 
279
- our_result = results_df[results_df['url'] == row['page']]
280
- our_rank = our_result['position'].values[0]
281
- total_results = len(results_df)
282
- our_score = our_result['relevancy_score'].values[0]
283
 
284
- st.write(f"Our page ranks {our_rank} out of {total_results} in Google search results.")
285
- st.write(f"Our relevancy score: {our_score:.4f}")
286
 
287
- if our_rank == 1:
288
- st.success("Your page has the highest position in Google search results!")
289
- elif our_rank <= 3:
290
- st.info("Your page is among the top 3 Google search results.")
291
- elif our_rank > total_results / 2:
292
- st.warning("Your page's position is in the lower half of the Google search results. Consider optimizing your content for better visibility.")
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
  def process_gsc_data(df):
295
  #logging.info("Processing GSC data")
 
254
  our_position = next((r['position'] for r in results if r['url'] == our_url), len(results) + 1)
255
  results.append({'position': our_position, 'url': our_url, 'relevancy_score': our_score})
256
 
257
+ # Sort results by the original position
258
+ results = sorted(results, key=lambda x: x['position'])
259
+
260
+ # Re-number positions starting from 1 and format URLs
261
+ for idx, item in enumerate(results, start=1):
262
+ item['position'] = idx
263
+ if item['url'] == our_url:
264
+ item['url'] = f"**{item['url']}**" # Mark our URL in bold
265
+
266
+ # Keep only 'position', 'url', and 'relevancy_score' columns
267
+ results_df = pd.DataFrame(results)[['position', 'url', 'relevancy_score']]
268
 
269
  return results_df
270
 
 
274
  with st.spinner('Analyzing competitors...'):
275
  results_df = analyze_competitors(row, co, country_code=country_code)
276
 
277
+ # Rename columns to have a proper header
278
+ results_df.columns = ['Position', 'URL', 'Score']
 
 
 
 
 
279
 
280
+ # Convert 'Position' to integer for proper sorting and display
281
+ results_df['Position'] = results_df['Position'].astype(int)
 
282
 
283
+ # Convert the DataFrame to Markdown with bold URLs where applicable
284
+ markdown_table = results_df.to_markdown(index=False)
 
 
285
 
286
+ # Display the Markdown table
287
+ st.markdown(markdown_table, unsafe_allow_html=True)
288
 
289
+ # Extract our result for additional insights
290
+ our_result = results_df[results_df['URL'].str.contains("**" + row['page'] + "**")]
291
+ if not our_result.empty:
292
+ our_rank = our_result['Position'].values[0]
293
+ total_results = len(results_df)
294
+ our_score = our_result['Score'].values[0]
295
+
296
+ st.write(f"Our page ranks **{our_rank}** out of **{total_results}** in Google search results.")
297
+ st.write(f"Our relevancy score: **{our_score:.4f}**")
298
+
299
+ if our_rank == 1:
300
+ st.success("Your page has the highest position in Google search results!")
301
+ elif our_rank <= 3:
302
+ st.info("Your page is among the top 3 Google search results.")
303
+ elif our_rank > total_results / 2:
304
+ st.warning("Your page's position is in the lower half of the Google search results. Consider optimizing your content for better visibility.")
305
+ else:
306
+ st.error("Our page was not found in the competitor analysis results.")
307
 
308
  def process_gsc_data(df):
309
  #logging.info("Processing GSC data")