poemsforaphrodite commited on
Commit
faa6271
·
verified ·
1 Parent(s): 7015859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -7
app.py CHANGED
@@ -489,8 +489,43 @@ def show_model_type_selector():
489
  def show_tabular_data(df, co):
490
  st.write("Data Table with Relevancy Scores")
491
 
492
- # Display the dataframe as a table
493
- st.dataframe(df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
 
495
  # Add an expander for each row to show competitor analysis
496
  for index, row in df.iterrows():
@@ -643,11 +678,7 @@ def main():
643
  st.session_state.report_data = fetch_gsc_data(webproperty, search_type, start_date, end_date, selected_dimensions)
644
 
645
  if st.session_state.report_data is not None and not st.session_state.report_data.empty:
646
- st.write("Data fetched successfully. Click the button below to calculate relevancy scores.")
647
-
648
- if st.button("Calculate Relevancy Scores"):
649
- # logger.info("Calculating relevancy scores for all rows")
650
- st.session_state.report_data = calculate_relevancy_scores(st.session_state.report_data, model_type)
651
 
652
  show_tabular_data(st.session_state.report_data, co)
653
 
 
489
  def show_tabular_data(df, co):
490
  st.write("Data Table with Relevancy Scores")
491
 
492
+ # Create a copy of the dataframe to modify
493
+ df_display = df.copy()
494
+
495
+ # Function to calculate relevancy score for a single row
496
+ def calculate_single_relevancy(row):
497
+ content = fetch_content(row['page'])
498
+ score = calculate_relevance_score(content, row['query'], co)
499
+ return score
500
+
501
+ # Function to update the relevancy score
502
+ def update_relevancy(row_index):
503
+ score = calculate_single_relevancy(df.iloc[row_index])
504
+ df.at[row_index, 'relevancy_score'] = score
505
+ df_display.at[row_index, 'relevancy_score'] = f"{score:.4f}"
506
+
507
+ # Create a column for the calculate button
508
+ df_display['calculate'] = ['Calculate' if pd.isna(score) or score == 0 else f"{score:.4f}"
509
+ for score in df['relevancy_score']]
510
+
511
+ # Display the dataframe
512
+ edited_df = st.data_editor(
513
+ df_display,
514
+ column_config={
515
+ "calculate": st.column_config.ButtonColumn(
516
+ "Relevancy Score",
517
+ help="Click to calculate relevancy score"
518
+ )
519
+ },
520
+ disabled=["page", "query", "clicks", "impressions", "ctr", "position"],
521
+ hide_index=True,
522
+ )
523
+
524
+ # Check if any button was clicked
525
+ for index, row in edited_df.iterrows():
526
+ if row['calculate'] == True: # Button was clicked
527
+ update_relevancy(index)
528
+ st.experimental_rerun()
529
 
530
  # Add an expander for each row to show competitor analysis
531
  for index, row in df.iterrows():
 
678
  st.session_state.report_data = fetch_gsc_data(webproperty, search_type, start_date, end_date, selected_dimensions)
679
 
680
  if st.session_state.report_data is not None and not st.session_state.report_data.empty:
681
+ st.write("Data fetched successfully.")
 
 
 
 
682
 
683
  show_tabular_data(st.session_state.report_data, co)
684