Table of Contents
Google PageSpeed Insights Module
Overview
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.
Features
- 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.
Installation
To use this module, you need to have the following Python packages installed:
requestsstreamlitpandasplotlytenacity
You can install these packages using pip:
pip install requests streamlit pandas plotly tenacity
Usage
Function: run_pagespeed
Fetches and processes PageSpeed Insights data.
Parameters:
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'.
Returns:
data(dict): The PageSpeed Insights data.
Example:
data = run_pagespeed("https://www.example.com", api_key="YOUR_API_KEY", strategy='MOBILE', locale='en')
Function: display_results
Presents PageSpeed Insights data in a user-friendly format using Streamlit.
Parameters:
data(dict): The PageSpeed Insights data.
Example:
display_results(data)
Function: google_pagespeed_insights
A Streamlit application that collects user input and displays PageSpeed Insights data.
Example:
if __name__ == "__main__":
google_pagespeed_insights()
Components
run_pagespeed
- Constructs the API request URL.
- Fetches data from the Google PageSpeed Insights API.
- Handles exceptions and displays errors using Streamlit.
display_results
- 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.
google_pagespeed_insights
- Collects user input through a Streamlit form.
- Calls
run_pagespeedto fetch data. - Calls
display_resultsto present the data.
Example
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.")
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request to contribute to this project.
