poemsforaphrodite commited on
Commit
b4a2f4a
·
verified ·
1 Parent(s): 0660abc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -10
app.py CHANGED
@@ -558,17 +558,76 @@ def show_dimensions_selector(search_type):
558
  )
559
 
560
  def show_paginated_dataframe(report, rows_per_page=20):
561
- # ... (rest of the function remains unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
 
563
- # Add this function to fetch page titles
564
- def get_page_title(url):
565
- try:
566
- response = requests.get(url, timeout=5)
567
- soup = BeautifulSoup(response.text, 'html.parser')
568
- title = soup.title.string if soup.title else None
569
- return title.strip() if title else None
570
- except:
571
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
 
573
  # -------------
574
  # Main Streamlit App Function
 
558
  )
559
 
560
  def show_paginated_dataframe(report, rows_per_page=20):
561
+ # logging.info("Showing paginated dataframe")
562
+ report['position'] = report['position'].astype(int)
563
+ report['impressions'] = pd.to_numeric(report['impressions'], errors='coerce')
564
+
565
+ def format_ctr(x):
566
+ try:
567
+ return f"{float(x):.2%}"
568
+ except ValueError:
569
+ return x
570
+
571
+ def format_relevancy_score(x):
572
+ try:
573
+ return f"{float(x):.2f}"
574
+ except ValueError:
575
+ return x
576
+
577
+ report['ctr'] = report['ctr'].apply(format_ctr)
578
+ report['relevancy_score'] = report['relevancy_score'].apply(format_relevancy_score)
579
+
580
+ def truncate_url(url, max_length=30):
581
+ return url[:max_length] + '...' if len(url) > max_length else url
582
 
583
+ def make_clickable(url):
584
+ truncated_url = truncate_url(url)
585
+ return f'<a href="{url}" target="_blank">{truncated_url}</a>'
586
+
587
+ report['clickable_url'] = report['page'].apply(make_clickable)
588
+
589
+ columns = ['clickable_url', 'query', 'impressions', 'clicks', 'ctr', 'position', 'relevancy_score']
590
+ report = report[columns]
591
+
592
+ sort_column = st.selectbox("Sort by:", columns[1:], index=columns[1:].index('impressions'))
593
+ sort_order = st.radio("Sort order:", ("Descending", "Ascending"))
594
+
595
+ ascending = sort_order == "Ascending"
596
+
597
+ def safe_float_convert(x):
598
+ try:
599
+ return float(x.rstrip('%')) / 100 if isinstance(x, str) and x.endswith('%') else float(x)
600
+ except ValueError:
601
+ return 0
602
+
603
+ report['ctr_numeric'] = report['ctr'].apply(safe_float_convert)
604
+ report['relevancy_score_numeric'] = report['relevancy_score'].apply(safe_float_convert)
605
+
606
+ sort_column_numeric = sort_column + '_numeric' if sort_column in ['ctr', 'relevancy_score'] else sort_column
607
+ report = report.sort_values(by=sort_column_numeric, ascending=ascending)
608
+
609
+ report = report.drop(columns=['ctr_numeric', 'relevancy_score_numeric'])
610
+
611
+ total_rows = len(report)
612
+ total_pages = (total_rows - 1) // rows_per_page + 1
613
+
614
+ if 'current_page' not in st.session_state:
615
+ st.session_state.current_page = 1
616
+
617
+ col1, col2, col3 = st.columns([1,3,1])
618
+ with col1:
619
+ if st.button("Previous", disabled=st.session_state.current_page == 1):
620
+ st.session_state.current_page -= 1
621
+ with col2:
622
+ st.write(f"Page {st.session_state.current_page} of {total_pages}")
623
+ with col3:
624
+ if st.button("Next", disabled=st.session_state.current_page == total_pages):
625
+ st.session_state.current_page += 1
626
+
627
+ start_idx = (st.session_state.current_page - 1) * rows_per_page
628
+ end_idx = start_idx + rows_per_page
629
+
630
+ st.markdown(report.iloc[start_idx:end_idx].to_html(escape=False, index=False), unsafe_allow_html=True)
631
 
632
  # -------------
633
  # Main Streamlit App Function