import os import streamlit as st from github import Github from github import GithubException from altair import Chart # Set up the Streamlit app st.set_page_config(page_title="GitHub File Search", page_icon=":mag_right:", theme="dark") # Retrieve the GitHub API token from the environment variable access_token = os.environ["GITHUB_TOKEN"] # Define a function to get all the repositories that contain a file with the given name def get_repositories_with_file(filename): # Create a GitHub client g = Github(access_token) # Search for the file on GitHub query = f"filename:{filename}" repositories = g.search_repositories(query=query, sort="stars", order="desc") # Initialize a list to store the repository information repo_info = [] # Loop through the repositories for repo in repositories: # Get the contents of the repository contents = repo.get_contents("") # Check if the repository contains a file with the given name for content in contents: if content.name == filename: repo_info.append({ "name": repo.name, "description": repo.description, "url": repo.html_url }) break # Return the repository information return repo_info # Define the Streamlit app def app(): # Set up the user interface st.title("GitHub File Search") st.write("Enter a file name to search for on GitHub:") filename = st.text_input("File name") if filename: # Get the repositories with the file repo_info = get_repositories_with_file(filename) # Display the repository information if len(repo_info) > 0: st.success(f"Found {len(repo_info)} repositories with the file '{filename}':") for repo in repo_info: st.write(f"- **{repo['name']}**: {repo['description']}") st.write(f" URL: [{repo['url']}]({repo['url']})") else: st.warning(f"No repositories found with the file '{filename}'.") # Create a chart to show the number of repositories found for each file name chart = Chart(data=repo_info).mark_bar().encode( x='name', y='count', color='name' ) # Display the chart st.plotly_chart(chart) # Run the Streamlit app if __name__ == "__main__": app()