poemsforaphrodite commited on
Commit
6a06f9a
1 Parent(s): baf3211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -65
app.py CHANGED
@@ -300,6 +300,8 @@ def show_dimensions_selector(search_type):
300
 
301
 
302
  def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
 
 
303
  # Check if required columns are present
304
  required_columns = ['page', 'query', 'clicks', 'impressions', 'ctr', 'position']
305
  missing_columns = [col for col in required_columns if col not in report.columns]
@@ -317,45 +319,18 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
317
  except ValueError:
318
  return x
319
 
320
- def format_relevancy_score(x):
321
- if x is None:
322
- return '<button class="calculate-btn">Calculate</button>'
323
- try:
324
- return f"{float(x):.2f}"
325
- except ValueError:
326
- return x
327
-
328
  report['ctr'] = report['ctr'].apply(format_ctr)
329
  if 'relevancy_score' not in report.columns:
330
  report['relevancy_score'] = None
331
- report['relevancy_score'] = report['relevancy_score'].apply(format_relevancy_score)
332
 
333
- def make_clickable(url):
334
- return f'<a href="{url}" target="_blank">{url}</a>'
335
-
336
- report['clickable_url'] = report['page'].apply(make_clickable)
337
-
338
- columns = ['clickable_url', 'query', 'impressions', 'clicks', 'ctr', 'position', 'relevancy_score']
339
  report = report[columns]
340
 
341
- sort_column = st.selectbox("Sort by:", columns[1:], index=columns[1:].index('impressions'))
342
  sort_order = st.radio("Sort order:", ("Descending", "Ascending"))
343
 
344
  ascending = sort_order == "Ascending"
345
-
346
- def safe_float_convert(x):
347
- try:
348
- return float(x.rstrip('%')) / 100 if isinstance(x, str) and x.endswith('%') else float(x)
349
- except ValueError:
350
- return 0
351
-
352
- report['ctr_numeric'] = report['ctr'].apply(safe_float_convert)
353
- report['relevancy_score_numeric'] = report['relevancy_score'].apply(lambda x: safe_float_convert(x) if x != '<button class="calculate-btn">Calculate</button>' else -1)
354
-
355
- sort_column_numeric = sort_column + '_numeric' if sort_column in ['ctr', 'relevancy_score'] else sort_column
356
- report = report.sort_values(by=sort_column_numeric, ascending=ascending)
357
-
358
- report = report.drop(columns=['ctr_numeric', 'relevancy_score_numeric'])
359
 
360
  total_rows = len(report)
361
  total_pages = (total_rows - 1) // rows_per_page + 1
@@ -376,44 +351,40 @@ def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
376
  start_idx = (st.session_state.current_page - 1) * rows_per_page
377
  end_idx = start_idx + rows_per_page
378
 
379
- # Create a placeholder for the dataframe
380
- dataframe_placeholder = st.empty()
381
-
382
- # Function to update the dataframe
383
- def update_dataframe():
384
- df_html = report.iloc[start_idx:end_idx].to_html(escape=False, index=False)
385
- df_html = df_html.replace('</table>', ''.join([
386
- '<style>',
387
- '.calculate-btn { cursor: pointer; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; }',
388
- '.calculate-btn:hover { background-color: #45a049; }',
389
- '</style>',
390
- *[f'<script>document.getElementsByTagName("table")[0].rows[{i+1}].cells[6].onclick = function(e) {{ if(e.target.classList.contains("calculate-btn")) {{ Streamlit.setComponentValue("calculate_relevancy", {start_idx + i}); }} }}</script>'
391
- for i in range(min(rows_per_page, len(report) - start_idx))]
392
- ]) + '</table>')
393
- dataframe_placeholder.markdown(df_html, unsafe_allow_html=True)
394
 
395
- # Initial dataframe display
396
- update_dataframe()
397
-
398
- # Handle relevancy calculation
399
- if st.session_state.get('calculate_relevancy') is not None:
400
- row_index = st.session_state.calculate_relevancy
401
- logger.info(f"Calculating relevancy for row index: {row_index}")
402
- try:
403
- page_content = fetch_content(report.iloc[row_index]['page'])
404
- query = report.iloc[row_index]['query']
405
- relevancy_score = calculate_single_relevancy_score(page_content, query, model_type)
406
- logger.info(f"Relevancy score calculated: {relevancy_score}")
407
- report.at[row_index, 'relevancy_score'] = f"{relevancy_score:.2f}"
408
- st.session_state.calculate_relevancy = None # Reset the state
409
- update_dataframe() # Update the dataframe display
410
- st.success(f"Relevancy score calculated for row {row_index + 1}")
411
- except Exception as e:
412
- logger.error(f"Error calculating relevancy score: {str(e)}")
413
- st.error(f"Error calculating relevancy score: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
414
 
415
  return report
416
-
417
  # -------------
418
  # Main Streamlit App Function
419
  # -------------
 
300
 
301
 
302
  def show_paginated_dataframe(report, rows_per_page=20, model_type='english'):
303
+ logger.info("Displaying paginated dataframe")
304
+
305
  # Check if required columns are present
306
  required_columns = ['page', 'query', 'clicks', 'impressions', 'ctr', 'position']
307
  missing_columns = [col for col in required_columns if col not in report.columns]
 
319
  except ValueError:
320
  return x
321
 
 
 
 
 
 
 
 
 
322
  report['ctr'] = report['ctr'].apply(format_ctr)
323
  if 'relevancy_score' not in report.columns:
324
  report['relevancy_score'] = None
 
325
 
326
+ columns = ['page', 'query', 'impressions', 'clicks', 'ctr', 'position', 'relevancy_score']
 
 
 
 
 
327
  report = report[columns]
328
 
329
+ sort_column = st.selectbox("Sort by:", columns, index=columns.index('impressions'))
330
  sort_order = st.radio("Sort order:", ("Descending", "Ascending"))
331
 
332
  ascending = sort_order == "Ascending"
333
+ report = report.sort_values(by=sort_column, ascending=ascending)
 
 
 
 
 
 
 
 
 
 
 
 
 
334
 
335
  total_rows = len(report)
336
  total_pages = (total_rows - 1) // rows_per_page + 1
 
351
  start_idx = (st.session_state.current_page - 1) * rows_per_page
352
  end_idx = start_idx + rows_per_page
353
 
354
+ page_data = report.iloc[start_idx:end_idx].reset_index(drop=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
 
356
+ for idx, row in page_data.iterrows():
357
+ col1, col2, col3, col4, col5, col6, col7, col8 = st.columns([3, 2, 1, 1, 1, 1, 1, 1])
358
+ with col1:
359
+ st.write(f"[{row['page']}]({row['page']})")
360
+ with col2:
361
+ st.write(row['query'])
362
+ with col3:
363
+ st.write(row['impressions'])
364
+ with col4:
365
+ st.write(row['clicks'])
366
+ with col5:
367
+ st.write(row['ctr'])
368
+ with col6:
369
+ st.write(row['position'])
370
+ with col7:
371
+ st.write(row['relevancy_score'] if row['relevancy_score'] is not None else "N/A")
372
+ with col8:
373
+ if st.button("Calculate", key=f"calc_{idx}"):
374
+ logger.info(f"Calculating relevancy for row index: {start_idx + idx}")
375
+ try:
376
+ page_content = fetch_content(row['page'])
377
+ query = row['query']
378
+ relevancy_score = calculate_single_relevancy_score(page_content, query, model_type)
379
+ logger.info(f"Relevancy score calculated: {relevancy_score}")
380
+ report.at[start_idx + idx, 'relevancy_score'] = f"{relevancy_score:.2f}"
381
+ st.success(f"Relevancy score calculated for row {start_idx + idx + 1}")
382
+ st.experimental_rerun()
383
+ except Exception as e:
384
+ logger.error(f"Error calculating relevancy score: {str(e)}")
385
+ st.error(f"Error calculating relevancy score: {str(e)}")
386
 
387
  return report
 
388
  # -------------
389
  # Main Streamlit App Function
390
  # -------------