poemsforaphrodite commited on
Commit
53455b9
1 Parent(s): 7b55067

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -93
app.py CHANGED
@@ -17,7 +17,6 @@ import requests
17
  from bs4 import BeautifulSoup
18
 
19
  load_dotenv()
20
- #test
21
 
22
  # Initialize Cohere client
23
  COHERE_API_KEY = os.environ["COHERE_API_KEY"]
@@ -48,8 +47,7 @@ DF_PREVIEW_ROWS = 100
48
 
49
  def setup_streamlit():
50
  st.set_page_config(page_title="Simple Google Search Console Data", layout="wide")
51
- st.title("GSC Relenvacy Score Calculator")
52
- # st.markdown(f"### Lightweight GSC Data Extractor. (Max {MAX_ROWS:,} Rows)")
53
  st.divider()
54
 
55
  def init_session_state():
@@ -246,95 +244,67 @@ def download_csv_link(report):
246
  # -------------
247
 
248
  def show_google_sign_in(auth_url):
249
- with st.sidebar:
250
- if st.button("Sign in with Google"):
251
- st.write('Please click the link below to sign in:')
252
- st.markdown(f'[Google Sign-In]({auth_url})', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
253
 
254
  def show_property_selector(properties, account):
255
- selected_property = st.selectbox(
256
- "Select a Search Console Property:",
257
- properties,
258
- index=properties.index(
259
- st.session_state.selected_property) if st.session_state.selected_property in properties else 0,
260
- key='selected_property_selector',
261
- on_change=property_change
262
- )
263
- return account[selected_property]
264
 
265
  def show_search_type_selector():
266
- return st.selectbox(
267
- "Select Search Type:",
268
- SEARCH_TYPES,
269
- index=SEARCH_TYPES.index(st.session_state.selected_search_type),
270
- key='search_type_selector'
271
- )
272
-
273
- def show_model_type_selector():
274
- return st.selectbox(
275
- "Select the embedding model:",
276
- ["english", "multilingual"],
277
- key='model_type_selector'
278
- )
279
 
280
  def show_date_range_selector():
281
- return st.selectbox(
282
- "Select Date Range:",
283
- DATE_RANGE_OPTIONS,
284
- index=DATE_RANGE_OPTIONS.index(st.session_state.selected_date_range),
285
- key='date_range_selector'
286
- )
287
 
288
  def show_custom_date_inputs():
289
- st.session_state.custom_start_date = st.date_input("Start Date", st.session_state.custom_start_date)
290
- st.session_state.custom_end_date = st.date_input("End Date", st.session_state.custom_end_date)
 
 
 
 
 
 
 
291
 
292
  def show_dimensions_selector(search_type):
293
- available_dimensions = update_dimensions(search_type)
294
- return st.multiselect(
295
- "Select Dimensions:",
296
- available_dimensions,
297
- default=st.session_state.selected_dimensions,
298
- key='dimensions_selector'
299
- )
 
 
 
 
 
 
 
 
 
300
 
301
- def show_paginated_dataframe(report, rows_per_page=20):
302
- # Convert 'position' column to integer
303
- report['position'] = report['position'].astype(int)
304
-
305
- # Create a clickable URL column
306
- def make_clickable(url):
307
- return f'<a href="{url}" target="_blank">{url}</a>'
308
-
309
- report['clickable_url'] = report['page'].apply(make_clickable)
310
-
311
- # Reorder columns to put clickable_url first and sort by impressions
312
- columns = ['clickable_url', 'query', 'impressions', 'clicks', 'ctr', 'position', 'relevancy_score']
313
- report = report[columns].sort_values('impressions', ascending=False)
314
-
315
- total_rows = len(report)
316
- total_pages = (total_rows - 1) // rows_per_page + 1
317
-
318
- if 'current_page' not in st.session_state:
319
- st.session_state.current_page = 1
320
-
321
- col1, col2, col3 = st.columns([1,3,1])
322
- with col1:
323
- if st.button("Previous", disabled=st.session_state.current_page == 1):
324
- st.session_state.current_page -= 1
325
- with col2:
326
- st.write(f"Page {st.session_state.current_page} of {total_pages}")
327
- with col3:
328
- if st.button("Next", disabled=st.session_state.current_page == total_pages):
329
- st.session_state.current_page += 1
330
-
331
- start_idx = (st.session_state.current_page - 1) * rows_per_page
332
- end_idx = start_idx + rows_per_page
333
-
334
- # Use st.markdown to display the dataframe with clickable links
335
- st.markdown(report.iloc[start_idx:end_idx].to_html(escape=False, index=False), unsafe_allow_html=True)
336
  # -------------
337
- # Main Streamlit App Function
338
  # -------------
339
 
340
  def main():
@@ -345,18 +315,10 @@ def main():
345
  st.session_state.auth_flow, st.session_state.auth_url = google_auth(client_config)
346
 
347
  # Retrieve query parameters
348
- query_params = st.query_params
349
- print("Query Parameters:", query_params)
350
-
351
- # Display the query parameters in the Streamlit app for debugging
352
- st.write("Query Parameters:", query_params)
353
-
354
- # Extract the authorization code from query parameters
355
  auth_code = query_params.get('code', [None])[0]
356
- print("Authorization Code:", auth_code)
357
-
358
- # Display the authorization code in the Streamlit app for debugging
359
- st.write("Authorization Code:", auth_code)
360
 
361
  if auth_code and 'credentials' not in st.session_state:
362
  try:
@@ -364,8 +326,7 @@ def main():
364
  st.session_state.credentials = st.session_state.auth_flow.credentials
365
  except Exception as e:
366
  st.error(f"Error fetching token: {e}")
367
- print(f"Error fetching token: {e}")
368
-
369
  if 'credentials' not in st.session_state:
370
  show_google_sign_in(st.session_state.auth_url)
371
  else:
 
17
  from bs4 import BeautifulSoup
18
 
19
  load_dotenv()
 
20
 
21
  # Initialize Cohere client
22
  COHERE_API_KEY = os.environ["COHERE_API_KEY"]
 
47
 
48
  def setup_streamlit():
49
  st.set_page_config(page_title="Simple Google Search Console Data", layout="wide")
50
+ st.title("GSC Relevancy Score Calculator")
 
51
  st.divider()
52
 
53
  def init_session_state():
 
244
  # -------------
245
 
246
  def show_google_sign_in(auth_url):
247
+ with elements("sign_in"):
248
+ st.markdown(
249
+ f"""
250
+ <div style="text-align: center;">
251
+ <h3>Google Search Console Sign-In</h3>
252
+ <a href="{auth_url}">
253
+ <button>Sign in with Google</button>
254
+ </a>
255
+ </div>
256
+ """,
257
+ unsafe_allow_html=True
258
+ )
259
 
260
  def show_property_selector(properties, account):
261
+ with st.sidebar:
262
+ st.subheader("Select a Search Console Property")
263
+ property_selector = st.selectbox("Available Properties", properties, key='selected_property_selector', on_change=property_change)
264
+ selected_property = next(prop for prop in account if prop.url == property_selector)
265
+ return selected_property
 
 
 
 
266
 
267
  def show_search_type_selector():
268
+ with st.sidebar:
269
+ st.subheader("Select Search Type")
270
+ return st.radio("Search Type", SEARCH_TYPES, key='selected_search_type')
 
 
 
 
 
 
 
 
 
 
271
 
272
  def show_date_range_selector():
273
+ with st.sidebar:
274
+ st.subheader("Select Date Range")
275
+ return st.radio("Date Range", DATE_RANGE_OPTIONS, key='selected_date_range')
 
 
 
276
 
277
  def show_custom_date_inputs():
278
+ with st.sidebar:
279
+ st.subheader("Select Custom Date Range")
280
+ st.session_state.custom_start_date = st.date_input("Start Date", value=st.session_state.start_date)
281
+ st.session_state.custom_end_date = st.date_input("End Date", value=st.session_state.end_date)
282
+
283
+ def show_model_type_selector():
284
+ with st.sidebar:
285
+ st.subheader("Select the Embedding Model")
286
+ return st.radio("Embedding Model", ["english", "multilingual"])
287
 
288
  def show_dimensions_selector(search_type):
289
+ dimensions = update_dimensions(search_type)
290
+ with st.sidebar:
291
+ st.subheader("Select Dimensions")
292
+ selected_dimensions = st.multiselect("Dimensions", dimensions, default=['page', 'query'])
293
+ return selected_dimensions
294
+
295
+ def show_device_selector():
296
+ with st.sidebar:
297
+ st.subheader("Select Device Type")
298
+ return st.selectbox("Device Type", DEVICE_OPTIONS, key='selected_device')
299
+
300
+ def show_paginated_dataframe(df):
301
+ if df.empty:
302
+ st.warning("No data available to display.")
303
+ else:
304
+ show_dataframe(df)
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  # -------------
307
+ # Main Function
308
  # -------------
309
 
310
  def main():
 
315
  st.session_state.auth_flow, st.session_state.auth_url = google_auth(client_config)
316
 
317
  # Retrieve query parameters
318
+ query_params = st.experimental_get_query_params() if IS_LOCAL else st.query_params
319
+ st.write("Query Parameters:", query_params) # Debugging
 
 
 
 
 
320
  auth_code = query_params.get('code', [None])[0]
321
+ st.write("Authorization Code:", auth_code) # Debugging
 
 
 
322
 
323
  if auth_code and 'credentials' not in st.session_state:
324
  try:
 
326
  st.session_state.credentials = st.session_state.auth_flow.credentials
327
  except Exception as e:
328
  st.error(f"Error fetching token: {e}")
329
+
 
330
  if 'credentials' not in st.session_state:
331
  show_google_sign_in(st.session_state.auth_url)
332
  else: