by Barandev
Get (List) All Repositories using GitHub API
import os
import requests
from flask import Flask, request, render_template
app = Flask(__name__)
GITHUB_API_URL = "https://api.github.com/users/{}/repos"
GITHUB_API_TOKEN = os.environ['GITHUB_API_TOKEN']
def fetch_repositories(username):
headers = {"Authorization": f"token {GITHUB_API_TOKEN}"}
response = requests.get(GITHUB_API_URL.format(username), headers=headers)
if response.status_code == 200:
repos = response.json()
for repo in repos:
# Manually parse and reshape the "last updated" information
last_updated = repo.get('updated_at')
if last_updated:
# Splitting the date and time components
date, time = last_updated.split('T')
year, month, day = date.split('-')
repo['updated_at'] = f"{day}-{month}-{year}" # Reshaping to DD-MM-YYYY format
return repos
else:
Created: | Last Updated:
Introduction to the GitHub Repositories Viewer Template
Welcome to the GitHub Repositories Viewer Template! This template allows you to create a simple web application that fetches and displays a list of GitHub repositories for a given user or organization. It's a great way to showcase your projects or to keep track of repositories you're interested in. The application uses the GitHub API to retrieve repository data and presents it in a user-friendly interface.
Getting Started with the Template
To begin using this template, click on "Start with this Template" on the Lazy platform. This will set up the template in your Lazy Builder interface, pre-populating the code so you can customize it as needed without any copy-pasting.
Initial Setup: Adding Environment Secrets
Before you can use the GitHub Repositories Viewer, you'll need to set up an environment secret for the GitHub API token. This token is necessary for the application to authenticate with the GitHub API and fetch repository data.
- Go to the GitHub Developer Settings to generate a new Personal Access Token (PAT).
- Select the appropriate scopes for your token. For this application, you'll need at least the
public_repo
scope. - Once you have your token, go to the Environment Secrets tab within the Lazy Builder.
- Create a new secret with the key
GITHUB_API_TOKEN
and paste your GitHub Personal Access Token as the value.
Test: Deploying the App
With your environment secret in place, you're ready to deploy the app. Press the "Test" button on the Lazy platform. This will begin the deployment process and launch the Lazy CLI. You won't need to provide any additional input at this stage, as the user input will be handled through the web interface once the app is running.
Using the App
After pressing the "Test" button, Lazy will provide you with a dedicated server link to access your application. Navigate to this link to view the GitHub Repositories Viewer interface.
Here's how to use the interface:
- Enter a GitHub username or organization name into the text field provided.
- Click the "Get Repositories" button to fetch and display the list of repositories.
- If you wish to reset the table and clear the current results, click the "Reset Table" button.
If the application encounters any issues fetching repositories, such as an invalid GitHub API token, it will alert you through the interface.
Integrating the App
If you wish to integrate this GitHub Repositories Viewer into another service or frontend, you can use the server link provided by Lazy as the endpoint for your requests. For example, you could embed the viewer into a personal website or a dashboard by creating an iframe that points to the server link.
Here's a sample code snippet for embedding the viewer:
<iframe src="YOUR_SERVER_LINK" width="100%" height="600"></iframe>
Replace YOUR_SERVER_LINK
with the actual link provided by Lazy after deployment.
By following these steps, you should now have a fully functional GitHub Repositories Viewer that you can use and integrate as you see fit. Enjoy showcasing your GitHub projects!