poemsforaphrodite commited on
Commit
3250b31
1 Parent(s): 91e9e9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -107,6 +107,7 @@ def process_gsc_data(df):
107
  result['relevancy_score'] = None # Initialize relevancy_score as None
108
  return result
109
 
 
110
  # -------------
111
  # Google Authentication Functions
112
  # -------------
@@ -298,7 +299,7 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
298
 
299
  def format_relevancy_score(x):
300
  if x is None:
301
- return "Not calculated"
302
  try:
303
  return f"{float(x):.2f}"
304
  except ValueError:
@@ -312,10 +313,10 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
312
 
313
  report['clickable_url'] = report['page'].apply(make_clickable)
314
 
315
- columns = ['clickable_url', 'query', 'impressions', 'clicks', 'ctr', 'position', 'relevancy_score', 'action']
316
- report = report[columns[:-1]] # Exclude 'action' from the DataFrame
317
 
318
- sort_column = st.selectbox("Sort by:", columns[1:-1], index=columns[1:-1].index('impressions'))
319
  sort_order = st.radio("Sort order:", ("Descending", "Ascending"))
320
 
321
  ascending = sort_order == "Ascending"
@@ -327,7 +328,7 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
327
  return 0
328
 
329
  report['ctr_numeric'] = report['ctr'].apply(safe_float_convert)
330
- report['relevancy_score_numeric'] = report['relevancy_score'].apply(lambda x: safe_float_convert(x) if x != "Not calculated" else -1)
331
 
332
  sort_column_numeric = sort_column + '_numeric' if sort_column in ['ctr', 'relevancy_score'] else sort_column
333
  report = report.sort_values(by=sort_column_numeric, ascending=ascending)
@@ -360,9 +361,8 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
360
  def update_dataframe():
361
  df_html = report.iloc[start_idx:end_idx].to_html(escape=False, index=False)
362
  df_html = df_html.replace('</table>', ''.join([
363
- '<th>Action</th></tr>',
364
- *[f'<tr><td colspan="7"></td><td><button onclick="calculate_relevancy({i})">Calculate Relevancy</button></td></tr>'
365
- for i in range(start_idx, min(end_idx, len(report)))]
366
  ]) + '</table>')
367
  dataframe_placeholder.markdown(df_html, unsafe_allow_html=True)
368
 
@@ -379,7 +379,7 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
379
  """, unsafe_allow_html=True)
380
 
381
  # Handle relevancy calculation
382
- if st.session_state.get('calculate_relevancy'):
383
  row_index = st.session_state.calculate_relevancy
384
  page_content = fetch_content(report.iloc[row_index]['page'])
385
  query = report.iloc[row_index]['query']
@@ -389,6 +389,7 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
389
  update_dataframe() # Update the dataframe display
390
 
391
  return report
 
392
  # -------------
393
  # Main Streamlit App Function
394
  # -------------
@@ -439,7 +440,7 @@ def main():
439
  st.session_state.report_data = fetch_gsc_data(webproperty, search_type, start_date, end_date, selected_dimensions)
440
 
441
  if st.session_state.report_data is not None and not st.session_state.report_data.empty:
442
- st.write("Data fetched successfully. Click the 'Calculate Relevancy' button in each row to calculate its relevancy score.")
443
  st.session_state.report_data = show_paginated_dataframe(st.session_state.report_data, model_type=model_type)
444
  download_csv_link(st.session_state.report_data)
445
  elif st.session_state.report_data is not None:
 
107
  result['relevancy_score'] = None # Initialize relevancy_score as None
108
  return result
109
 
110
+
111
  # -------------
112
  # Google Authentication Functions
113
  # -------------
 
299
 
300
  def format_relevancy_score(x):
301
  if x is None:
302
+ return "Calculate"
303
  try:
304
  return f"{float(x):.2f}"
305
  except ValueError:
 
313
 
314
  report['clickable_url'] = report['page'].apply(make_clickable)
315
 
316
+ columns = ['clickable_url', 'query', 'impressions', 'clicks', 'ctr', 'position', 'relevancy_score']
317
+ report = report[columns]
318
 
319
+ sort_column = st.selectbox("Sort by:", columns[1:], index=columns[1:].index('impressions'))
320
  sort_order = st.radio("Sort order:", ("Descending", "Ascending"))
321
 
322
  ascending = sort_order == "Ascending"
 
328
  return 0
329
 
330
  report['ctr_numeric'] = report['ctr'].apply(safe_float_convert)
331
+ report['relevancy_score_numeric'] = report['relevancy_score'].apply(lambda x: safe_float_convert(x) if x != "Calculate" else -1)
332
 
333
  sort_column_numeric = sort_column + '_numeric' if sort_column in ['ctr', 'relevancy_score'] else sort_column
334
  report = report.sort_values(by=sort_column_numeric, ascending=ascending)
 
361
  def update_dataframe():
362
  df_html = report.iloc[start_idx:end_idx].to_html(escape=False, index=False)
363
  df_html = df_html.replace('</table>', ''.join([
364
+ *[f'<script>document.getElementsByTagName("table")[0].rows[{i+1}].cells[6].onclick = function() {{ calculate_relevancy({start_idx + i}) }}</script>'
365
+ for i in range(min(rows_per_page, len(report) - start_idx))]
 
366
  ]) + '</table>')
367
  dataframe_placeholder.markdown(df_html, unsafe_allow_html=True)
368
 
 
379
  """, unsafe_allow_html=True)
380
 
381
  # Handle relevancy calculation
382
+ if st.session_state.get('calculate_relevancy') is not None:
383
  row_index = st.session_state.calculate_relevancy
384
  page_content = fetch_content(report.iloc[row_index]['page'])
385
  query = report.iloc[row_index]['query']
 
389
  update_dataframe() # Update the dataframe display
390
 
391
  return report
392
+
393
  # -------------
394
  # Main Streamlit App Function
395
  # -------------
 
440
  st.session_state.report_data = fetch_gsc_data(webproperty, search_type, start_date, end_date, selected_dimensions)
441
 
442
  if st.session_state.report_data is not None and not st.session_state.report_data.empty:
443
+ st.write("Data fetched successfully. Click 'Calculate' in the Relevancy Score column to calculate the score for each row.")
444
  st.session_state.report_data = show_paginated_dataframe(st.session_state.report_data, model_type=model_type)
445
  download_csv_link(st.session_state.report_data)
446
  elif st.session_state.report_data is not None: