Spaces:
Sleeping
Sleeping
poemsforaphrodite
commited on
Commit
•
fd6bb51
1
Parent(s):
53455b9
Update app.py
Browse files
app.py
CHANGED
@@ -17,6 +17,7 @@ import requests
|
|
17 |
from bs4 import BeautifulSoup
|
18 |
|
19 |
load_dotenv()
|
|
|
20 |
|
21 |
# Initialize Cohere client
|
22 |
COHERE_API_KEY = os.environ["COHERE_API_KEY"]
|
@@ -47,7 +48,8 @@ DF_PREVIEW_ROWS = 100
|
|
47 |
|
48 |
def setup_streamlit():
|
49 |
st.set_page_config(page_title="Simple Google Search Console Data", layout="wide")
|
50 |
-
st.title("
|
|
|
51 |
st.divider()
|
52 |
|
53 |
def init_session_state():
|
@@ -244,67 +246,95 @@ def download_csv_link(report):
|
|
244 |
# -------------
|
245 |
|
246 |
def show_google_sign_in(auth_url):
|
247 |
-
with
|
248 |
-
st.
|
249 |
-
|
250 |
-
|
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 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
266 |
|
267 |
def show_search_type_selector():
|
268 |
-
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
|
272 |
def show_date_range_selector():
|
273 |
-
|
274 |
-
|
275 |
-
|
|
|
|
|
|
|
276 |
|
277 |
def show_custom_date_inputs():
|
278 |
-
|
279 |
-
|
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 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
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():
|
@@ -314,19 +344,14 @@ def main():
|
|
314 |
if 'auth_flow' not in st.session_state or 'auth_url' not in st.session_state:
|
315 |
st.session_state.auth_flow, st.session_state.auth_url = google_auth(client_config)
|
316 |
|
317 |
-
|
318 |
-
query_params
|
319 |
-
|
320 |
-
|
321 |
-
st.write("Authorization Code:", auth_code) # Debugging
|
322 |
-
|
323 |
if auth_code and 'credentials' not in st.session_state:
|
324 |
-
|
325 |
-
|
326 |
-
|
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:
|
@@ -338,7 +363,7 @@ def main():
|
|
338 |
webproperty = show_property_selector(properties, account)
|
339 |
search_type = show_search_type_selector()
|
340 |
date_range_selection = show_date_range_selector()
|
341 |
-
model_type = show_model_type_selector()
|
342 |
if date_range_selection == 'Custom Range':
|
343 |
show_custom_date_inputs()
|
344 |
start_date, end_date = st.session_state.custom_start_date, st.session_state.custom_end_date
|
@@ -352,13 +377,14 @@ def main():
|
|
352 |
|
353 |
if st.button("Fetch Data"):
|
354 |
with st.spinner('Fetching data...'):
|
355 |
-
st.session_state.report_data = fetch_data_loading(webproperty, search_type, start_date, end_date, selected_dimensions, model_type=model_type)
|
356 |
|
357 |
if st.session_state.report_data is not None and not st.session_state.report_data.empty:
|
358 |
show_paginated_dataframe(st.session_state.report_data)
|
359 |
download_csv_link(st.session_state.report_data)
|
360 |
elif st.session_state.report_data is not None:
|
361 |
st.warning("No data found for the selected criteria.")
|
|
|
362 |
|
363 |
if __name__ == "__main__":
|
364 |
-
main()
|
|
|
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 |
|
49 |
def setup_streamlit():
|
50 |
st.set_page_config(page_title="Simple Google Search Console Data", layout="wide")
|
51 |
+
st.title("✨ Simple Google Search Console Data | June 2024")
|
52 |
+
st.markdown(f"### Lightweight GSC Data Extractor. (Max {MAX_ROWS:,} Rows)")
|
53 |
st.divider()
|
54 |
|
55 |
def init_session_state():
|
|
|
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():
|
|
|
344 |
if 'auth_flow' not in st.session_state or 'auth_url' not in st.session_state:
|
345 |
st.session_state.auth_flow, st.session_state.auth_url = google_auth(client_config)
|
346 |
|
347 |
+
query_params = st.experimental_get_query_params()
|
348 |
+
print(query_params)
|
349 |
+
auth_code = query_params.get("code", [None])[0]
|
350 |
+
|
|
|
|
|
351 |
if auth_code and 'credentials' not in st.session_state:
|
352 |
+
st.session_state.auth_flow.fetch_token(code=auth_code)
|
353 |
+
st.session_state.credentials = st.session_state.auth_flow.credentials
|
354 |
+
|
|
|
|
|
|
|
355 |
if 'credentials' not in st.session_state:
|
356 |
show_google_sign_in(st.session_state.auth_url)
|
357 |
else:
|
|
|
363 |
webproperty = show_property_selector(properties, account)
|
364 |
search_type = show_search_type_selector()
|
365 |
date_range_selection = show_date_range_selector()
|
366 |
+
model_type = show_model_type_selector() # Add this line
|
367 |
if date_range_selection == 'Custom Range':
|
368 |
show_custom_date_inputs()
|
369 |
start_date, end_date = st.session_state.custom_start_date, st.session_state.custom_end_date
|
|
|
377 |
|
378 |
if st.button("Fetch Data"):
|
379 |
with st.spinner('Fetching data...'):
|
380 |
+
st.session_state.report_data = fetch_data_loading(webproperty, search_type, start_date, end_date, selected_dimensions, model_type=model_type) # Update this line
|
381 |
|
382 |
if st.session_state.report_data is not None and not st.session_state.report_data.empty:
|
383 |
show_paginated_dataframe(st.session_state.report_data)
|
384 |
download_csv_link(st.session_state.report_data)
|
385 |
elif st.session_state.report_data is not None:
|
386 |
st.warning("No data found for the selected criteria.")
|
387 |
+
|
388 |
|
389 |
if __name__ == "__main__":
|
390 |
+
main()
|