Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,25 @@ import streamlit as st
|
|
| 2 |
import json
|
| 3 |
from googleapiclient.discovery import build
|
| 4 |
|
| 5 |
-
#
|
|
|
|
| 6 |
default_api_key = "AIzaSyAYdqy4sQzUPFXuSXVDJn3WHIEaHMXMv5I"
|
| 7 |
-
API_KEY = st.text_input(
|
| 8 |
"Enter your PageSpeed Insights API Key",
|
| 9 |
value=default_api_key,
|
| 10 |
help="Don't have one? Get your API key from [Google PageSpeed Insights Get Started](https://developers.google.com/speed/docs/insights/v5/get-started)."
|
| 11 |
)
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def run_pagespeed(service, url, strategy="DESKTOP"):
|
| 15 |
st.info(f"Running PageSpeed Insights for {url} with strategy {strategy}...")
|
| 16 |
try:
|
|
@@ -20,8 +30,11 @@ def run_pagespeed(service, url, strategy="DESKTOP"):
|
|
| 20 |
return None
|
| 21 |
return result
|
| 22 |
|
| 23 |
-
# Function to process the API result
|
| 24 |
def process_result(result):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
data = {}
|
| 26 |
try:
|
| 27 |
lighthouse_result = result.get("lighthouseResult", {})
|
|
@@ -37,23 +50,21 @@ def process_result(result):
|
|
| 37 |
st.error(f"Error processing result: {e}")
|
| 38 |
return data
|
| 39 |
|
| 40 |
-
# Main
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
strategy = st.radio("Select Strategy", ("DESKTOP", "MOBILE"), index=0)
|
| 47 |
-
|
| 48 |
-
if st.button("Run Analysis"):
|
| 49 |
-
# Build the PageSpeed Insights API service object
|
| 50 |
-
service = build('pagespeedonline', 'v5', developerKey=API_KEY)
|
| 51 |
-
with st.spinner("Running analysis..."):
|
| 52 |
-
result = run_pagespeed(service, url, strategy=strategy)
|
| 53 |
-
if result:
|
| 54 |
-
metrics = process_result(result)
|
| 55 |
-
st.subheader("Extracted Metrics")
|
| 56 |
-
st.write(metrics)
|
| 57 |
-
st.subheader("Full JSON Response")
|
| 58 |
-
st.json(result)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
from googleapiclient.discovery import build
|
| 4 |
|
| 5 |
+
# ---------------- Sidebar Configuration ----------------
|
| 6 |
+
st.sidebar.header("Configuration")
|
| 7 |
default_api_key = "AIzaSyAYdqy4sQzUPFXuSXVDJn3WHIEaHMXMv5I"
|
| 8 |
+
API_KEY = st.sidebar.text_input(
|
| 9 |
"Enter your PageSpeed Insights API Key",
|
| 10 |
value=default_api_key,
|
| 11 |
help="Don't have one? Get your API key from [Google PageSpeed Insights Get Started](https://developers.google.com/speed/docs/insights/v5/get-started)."
|
| 12 |
)
|
| 13 |
|
| 14 |
+
url = st.sidebar.text_input("URL", "https://www.ocoya.com/")
|
| 15 |
+
strategy = st.sidebar.radio("Select Strategy", ("DESKTOP", "MOBILE"), index=0)
|
| 16 |
+
|
| 17 |
+
st.sidebar.markdown("### About")
|
| 18 |
+
st.sidebar.info(
|
| 19 |
+
"This app uses the Google PageSpeed Insights API to analyze the performance of a web page. "
|
| 20 |
+
"You can use your own API key or the default key provided above."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# ---------------- Helper Functions ----------------
|
| 24 |
def run_pagespeed(service, url, strategy="DESKTOP"):
|
| 25 |
st.info(f"Running PageSpeed Insights for {url} with strategy {strategy}...")
|
| 26 |
try:
|
|
|
|
| 30 |
return None
|
| 31 |
return result
|
| 32 |
|
|
|
|
| 33 |
def process_result(result):
|
| 34 |
+
"""
|
| 35 |
+
Extract key metrics from the API response.
|
| 36 |
+
Scores are converted to percentages (0-100).
|
| 37 |
+
"""
|
| 38 |
data = {}
|
| 39 |
try:
|
| 40 |
lighthouse_result = result.get("lighthouseResult", {})
|
|
|
|
| 50 |
st.error(f"Error processing result: {e}")
|
| 51 |
return data
|
| 52 |
|
| 53 |
+
# ---------------- Main App Content ----------------
|
| 54 |
+
st.title("PageSpeed Insights Analyzer")
|
| 55 |
+
st.write(
|
| 56 |
+
"Use the sidebar to configure the analysis by entering a URL, selecting a strategy, and providing an API key. "
|
| 57 |
+
"Then click **Run Analysis** in the sidebar."
|
| 58 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
if st.sidebar.button("Run Analysis"):
|
| 61 |
+
# Build the API service object
|
| 62 |
+
service = build('pagespeedonline', 'v5', developerKey=API_KEY)
|
| 63 |
+
with st.spinner("Running analysis..."):
|
| 64 |
+
result = run_pagespeed(service, url, strategy=strategy)
|
| 65 |
+
if result:
|
| 66 |
+
metrics = process_result(result)
|
| 67 |
+
st.subheader("Extracted Metrics")
|
| 68 |
+
st.write(metrics)
|
| 69 |
+
st.subheader("Full JSON Response")
|
| 70 |
+
st.json(result)
|