poemsforaphrodite commited on
Commit
56e7b53
·
verified ·
1 Parent(s): 0978cb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -73
app.py CHANGED
@@ -240,68 +240,70 @@ def show_google_sign_in(auth_url):
240
  with st.sidebar:
241
  if st.button("Sign in with Google"):
242
  st.write('Please click the link below to sign in:')
243
- st.markdown(f'[Google Sign-In]({auth_url})')
244
-
245
- def show_property_selection(property_choices):
246
- with st.sidebar:
247
- selected_property = st.selectbox("Select a property", property_choices, key='selected_property_selector', on_change=property_change)
248
- st.session_state.selected_property = selected_property
249
- return selected_property
250
-
251
- def show_search_type_selection():
252
- with st.sidebar:
253
- search_type = st.selectbox("Select Search Type", SEARCH_TYPES, index=0)
254
- st.session_state.selected_search_type = search_type
255
- return search_type
256
-
257
- def show_date_range_selection():
258
- with st.sidebar:
259
- date_range = st.selectbox("Select Date Range", DATE_RANGE_OPTIONS, index=0)
260
- st.session_state.selected_date_range = date_range
261
- if date_range == "Custom Range":
262
- start_date = st.date_input("Start Date", st.session_state.custom_start_date)
263
- end_date = st.date_input("End Date", st.session_state.custom_end_date)
264
- st.session_state.start_date, st.session_state.end_date = start_date, end_date
265
- else:
266
- start_date, end_date = calc_date_range(date_range)
267
- st.session_state.start_date, st.session_state.end_date = start_date, end_date
268
- return start_date, end_date
269
 
270
- def show_dimension_selection():
271
- with st.sidebar:
272
- selected_dimensions = st.multiselect("Select Dimensions", update_dimensions(st.session_state.selected_search_type), default=BASE_DIMENSIONS)
273
- st.session_state.selected_dimensions = selected_dimensions
274
- return selected_dimensions
 
275
 
276
- def show_device_selection():
277
- with st.sidebar:
278
- selected_device = st.selectbox("Select Device", DEVICE_OPTIONS, index=0)
279
- st.session_state.selected_device = selected_device
280
- return selected_device
 
 
281
 
282
- def show_embedding_model_selection():
283
- with st.sidebar:
284
- selected_model = st.radio("Select Embedding Model Type", ("English", "Multilingual"), index=0)
285
- return selected_model.lower()
 
 
 
 
 
 
 
 
286
 
287
  def show_paginated_dataframe(report, rows_per_page=20):
288
  # Convert 'position' column to integer
289
  report['position'] = report['position'].astype(int)
290
 
291
- # Filter for queries below position 10 and sort by impressions in descending order
292
- filtered_report = report[report['position'] < 10].sort_values(by='impressions', ascending=False)
293
-
294
  # Create a clickable URL column
295
  def make_clickable(url):
296
  return f'<a href="{url}" target="_blank">{url}</a>'
297
 
298
- filtered_report['clickable_url'] = filtered_report['page'].apply(make_clickable)
299
 
300
  # Reorder columns to put clickable_url first
301
- columns = ['clickable_url'] + [col for col in filtered_report.columns if col != 'clickable_url' and col != 'page']
302
- filtered_report = filtered_report[columns]
303
 
304
- total_rows = len(filtered_report)
305
  total_pages = (total_rows - 1) // rows_per_page + 1
306
 
307
  if 'current_page' not in st.session_state:
@@ -321,41 +323,58 @@ def show_paginated_dataframe(report, rows_per_page=20):
321
  end_idx = start_idx + rows_per_page
322
 
323
  # Use st.markdown to display the dataframe with clickable links
324
- st.markdown(filtered_report.iloc[start_idx:end_idx].to_html(escape=False, index=False), unsafe_allow_html=True)
325
-
326
  # -------------
327
- # Main Function
328
  # -------------
329
 
330
  def main():
331
  setup_streamlit()
332
- init_session_state()
333
-
334
  client_config = load_config()
335
- flow, auth_url = google_auth(client_config)
336
 
 
 
 
 
 
 
 
 
 
 
337
  if 'credentials' not in st.session_state:
338
- show_google_sign_in(auth_url)
339
  else:
340
- credentials = st.session_state['credentials']
341
- account = auth_search_console(client_config, credentials)
342
- property_choices = list_gsc_properties(credentials)
343
- selected_property = show_property_selection(property_choices)
344
- search_type = show_search_type_selection()
345
- start_date, end_date = show_date_range_selection()
346
- selected_dimensions = show_dimension_selection()
347
- selected_device = show_device_selection()
348
- model_type = show_embedding_model_selection()
349
-
350
- if st.button("Fetch Data"):
351
- webproperty = account[selected_property]
352
- report = fetch_data_loading(webproperty, search_type, start_date, end_date, selected_dimensions, selected_device, model_type)
353
- if not report.empty:
354
- show_dataframe(report)
355
- show_paginated_dataframe(report)
356
- download_csv_link(report)
357
  else:
358
- st.write("No data found for the selected criteria.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
 
 
360
  if __name__ == "__main__":
361
- main()
 
240
  with st.sidebar:
241
  if st.button("Sign in with Google"):
242
  st.write('Please click the link below to sign in:')
243
+ st.markdown(f'[Google Sign-In]({auth_url})', unsafe_allow_html=True)
244
+
245
+ def show_property_selector(properties, account):
246
+ selected_property = st.selectbox(
247
+ "Select a Search Console Property:",
248
+ properties,
249
+ index=properties.index(
250
+ st.session_state.selected_property) if st.session_state.selected_property in properties else 0,
251
+ key='selected_property_selector',
252
+ on_change=property_change
253
+ )
254
+ return account[selected_property]
255
+
256
+ def show_search_type_selector():
257
+ return st.selectbox(
258
+ "Select Search Type:",
259
+ SEARCH_TYPES,
260
+ index=SEARCH_TYPES.index(st.session_state.selected_search_type),
261
+ key='search_type_selector'
262
+ )
 
 
 
 
 
 
263
 
264
+ def show_model_type_selector():
265
+ return st.selectbox(
266
+ "Select the embedding model:",
267
+ ["english", "multilingual"],
268
+ key='model_type_selector'
269
+ )
270
 
271
+ def show_date_range_selector():
272
+ return st.selectbox(
273
+ "Select Date Range:",
274
+ DATE_RANGE_OPTIONS,
275
+ index=DATE_RANGE_OPTIONS.index(st.session_state.selected_date_range),
276
+ key='date_range_selector'
277
+ )
278
 
279
+ def show_custom_date_inputs():
280
+ st.session_state.custom_start_date = st.date_input("Start Date", st.session_state.custom_start_date)
281
+ st.session_state.custom_end_date = st.date_input("End Date", st.session_state.custom_end_date)
282
+
283
+ def show_dimensions_selector(search_type):
284
+ available_dimensions = update_dimensions(search_type)
285
+ return st.multiselect(
286
+ "Select Dimensions:",
287
+ available_dimensions,
288
+ default=st.session_state.selected_dimensions,
289
+ key='dimensions_selector'
290
+ )
291
 
292
  def show_paginated_dataframe(report, rows_per_page=20):
293
  # Convert 'position' column to integer
294
  report['position'] = report['position'].astype(int)
295
 
 
 
 
296
  # Create a clickable URL column
297
  def make_clickable(url):
298
  return f'<a href="{url}" target="_blank">{url}</a>'
299
 
300
+ report['clickable_url'] = report['page'].apply(make_clickable)
301
 
302
  # Reorder columns to put clickable_url first
303
+ columns = ['clickable_url'] + [col for col in report.columns if col != 'clickable_url' and col != 'page']
304
+ report = report[columns]
305
 
306
+ total_rows = len(report)
307
  total_pages = (total_rows - 1) // rows_per_page + 1
308
 
309
  if 'current_page' not in st.session_state:
 
323
  end_idx = start_idx + rows_per_page
324
 
325
  # Use st.markdown to display the dataframe with clickable links
326
+ st.markdown(report.iloc[start_idx:end_idx].to_html(escape=False, index=False), unsafe_allow_html=True)
 
327
  # -------------
328
+ # Main Streamlit App Function
329
  # -------------
330
 
331
  def main():
332
  setup_streamlit()
 
 
333
  client_config = load_config()
 
334
 
335
+ if 'auth_flow' not in st.session_state or 'auth_url' not in st.session_state:
336
+ st.session_state.auth_flow, st.session_state.auth_url = google_auth(client_config)
337
+
338
+ query_params = st.experimental_get_query_params()
339
+ auth_code = query_params.get("code", [None])[0]
340
+
341
+ if auth_code and 'credentials' not in st.session_state:
342
+ st.session_state.auth_flow.fetch_token(code=auth_code)
343
+ st.session_state.credentials = st.session_state.auth_flow.credentials
344
+
345
  if 'credentials' not in st.session_state:
346
+ show_google_sign_in(st.session_state.auth_url)
347
  else:
348
+ init_session_state()
349
+ account = auth_search_console(client_config, st.session_state.credentials)
350
+ properties = list_gsc_properties(st.session_state.credentials)
351
+
352
+ if properties:
353
+ webproperty = show_property_selector(properties, account)
354
+ search_type = show_search_type_selector()
355
+ date_range_selection = show_date_range_selector()
356
+ model_type = show_model_type_selector() # Add this line
357
+ if date_range_selection == 'Custom Range':
358
+ show_custom_date_inputs()
359
+ start_date, end_date = st.session_state.custom_start_date, st.session_state.custom_end_date
 
 
 
 
 
360
  else:
361
+ start_date, end_date = calc_date_range(date_range_selection)
362
+
363
+ selected_dimensions = show_dimensions_selector(search_type)
364
+
365
+ if 'report_data' not in st.session_state:
366
+ st.session_state.report_data = None
367
+
368
+ if st.button("Fetch Data"):
369
+ with st.spinner('Fetching data...'):
370
+ st.session_state.report_data = fetch_data_loading(webproperty, search_type, start_date, end_date, selected_dimensions, model_type=model_type) # Update this line
371
+
372
+ if st.session_state.report_data is not None and not st.session_state.report_data.empty:
373
+ show_paginated_dataframe(st.session_state.report_data)
374
+ download_csv_link(st.session_state.report_data)
375
+ elif st.session_state.report_data is not None:
376
+ st.warning("No data found for the selected criteria.")
377
 
378
+
379
  if __name__ == "__main__":
380
+ main()