GitHub Commit Analysis with Streamlit

Essential AI Terms: Understand ChatGPT with These 47 Key Concepts

a Python script that analyzes GitHub commits on the “master” branch and uses Streamlit to display a bar chart of monthly commit numbers. This script will use the pygithub library to interact with the GitHub API and plotly for creating the chart, which integrates well with Streamlit.

This script does the following:

  1. It uses Streamlit to create a simple web interface where users can input the repository name and their GitHub personal access token.
  2. Once the inputs are provided, it uses the pygithub library to fetch commits from the specified repository’s master branch.
  3. It then processes the commit data, counting the number of commits for each month.
  4. Finally, it creates a bar chart using Plotly and displays it using Streamlit.

To run this script, you’ll need to install the required libraries. You can do this by running:

pip install streamlit pygithub plotly

Then, save the script to a file (e.g., github_analysis.py) and run it using:

streamlit run github_analysis.py

This will start a local Streamlit server, and you should see the app open in your default web browser.

Remember to use a GitHub personal access token with the necessary permissions to read repository data. Also, be aware that for large repositories, this script might take some time to run due to API rate limits and the potentially large number of commits to process.

Would you like me to explain any part of the code in more detail? CopyRetry

Claude does not have the ability to run the code it generates yet.

import streamlit as st
from github import Github
from collections import defaultdict
from datetime import datetime
import plotly.graph_objects as go

# Streamlit app
st.title("GitHub Commit Analysis")

# User inputs
repo_name = st.text_input("Enter the repository name (e.g., 'owner/repo'):")
github_token = st.text_input("Enter your GitHub personal access token:", type="password")

if repo_name and github_token:
    # Initialize GitHub client
    g = Github(github_token)

    try:
        # Get the repository
        repo = g.get_repo(repo_name)

        # Get commits from the master branch
        commits = repo.get_commits(sha="master")

        # Count commits by month
        commit_counts = defaultdict(int)
        for commit in commits:
            date = commit.commit.author.date
            month_year = date.strftime("%Y-%m")
            commit_counts[month_year] += 1

        # Sort the data by date
        sorted_data = sorted(commit_counts.items())
        months, counts = zip(*sorted_data)

        # Create a bar chart using Plotly
        fig = go.Figure(data=[go.Bar(x=months, y=counts)])
        fig.update_layout(
            title="Monthly Commits on Master Branch",
            xaxis_title="Month",
            yaxis_title="Number of Commits"
        )

        # Display the chart using Streamlit
        st.plotly_chart(fig)

    except Exception as e:
        st.error(f"An error occurred: {str(e)}")
else:
    st.info("Please enter the repository name and GitHub token to analyze commits.")

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top