-
Notifications
You must be signed in to change notification settings - Fork 110
ALwrity Google PageSpeed Insights AI Tool
The google_pagespeed_insights.py
module is designed to fetch and display detailed insights into a website's performance using Google PageSpeed Insights API. This module utilizes various libraries such as requests
, streamlit
, pandas
, and plotly.express
to retrieve and visualize the performance data.
- Fetches PageSpeed Insights data for a specified URL.
- Displays performance metrics including Performance, Accessibility, SEO, and Best Practices scores.
- Presents additional metrics such as First Contentful Paint (FCP), Largest Contentful Paint (LCP), Time to Interactive (TTI), Total Blocking Time (TBT), and Cumulative Layout Shift (CLS).
- Visualizes network requests and main thread work breakdown.
- Provides detailed audit results for various performance metrics.
To use this module, you need to have the following Python packages installed:
requests
streamlit
pandas
plotly
tenacity
You can install these packages using pip:
pip install requests streamlit pandas plotly tenacity
Fetches and processes PageSpeed Insights data.
-
url
(str): The URL of the website to analyze. -
api_key
(str, optional): Your Google API key. Default isNone
. -
strategy
(str): The strategy to use ('DESKTOP'
or'MOBILE'
). Default is'DESKTOP'
. -
locale
(str): The locale to use. Default is'en'
.
-
data
(dict): The PageSpeed Insights data.
data = run_pagespeed("https://www.example.com", api_key="YOUR_API_KEY", strategy='MOBILE', locale='en')
Presents PageSpeed Insights data in a user-friendly format using Streamlit.
-
data
(dict): The PageSpeed Insights data.
display_results(data)
A Streamlit application that collects user input and displays PageSpeed Insights data.
if __name__ == "__main__":
google_pagespeed_insights()
- Constructs the API request URL.
- Fetches data from the Google PageSpeed Insights API.
- Handles exceptions and displays errors using Streamlit.
- Extracts and displays performance scores.
- Presents additional metrics in a tabular format.
- Visualizes network requests and main thread work breakdown using Plotly.
- Displays detailed audit results for various performance metrics.
- Collects user input through a Streamlit form.
- Calls
run_pagespeed
to fetch data. - Calls
display_results
to present the data.
import streamlit as st
from google_pagespeed_insights import run_pagespeed, display_results
def google_pagespeed_insights():
st.markdown("<h1 style='text-align: center; color: #1565C0;'>PageSpeed Insights Analyzer</h1>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>Get detailed insights into your website's performance! Powered by Google PageSpeed Insights</h3>", unsafe_allow_html=True)
with st.form("pagespeed_form"):
url = st.text_input("Enter Website URL", placeholder="https://www.example.com")
api_key = st.text_input("Enter Google API Key (Optional)", placeholder="Your API Key", help="Get your API key here: [https://developers.google.com/speed/docs/insights/v5/get-started#key]")
device = st.selectbox("Choose Device", ["Mobile", "Desktop"])
locale = st.selectbox("Choose Locale", ["en", "fr", "es", "de", "ja"])
categories = st.multiselect("Select Categories to Analyze", ['PERFORMANCE', 'ACCESSIBILITY', 'BEST_PRACTICES', 'SEO'], default=['PERFORMANCE', 'ACCESSIBILITY', 'BEST_PRACTICES', 'SEO'])
submitted = st.form_submit_button("Analyze")
if submitted:
if not url:
st.error("Please provide the website URL.")
else:
strategy = 'mobile' if device == "Mobile" else 'desktop'
data = run_pagespeed(url, api_key, strategy=strategy, locale=locale)
if data:
display_results(data)
else:
st.error("Failed to retrieve PageSpeed Insights data.")
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Please open an issue or submit a pull request to contribute to this project.