by Barandev
Search Code Repository using GitHub API by Name or User using Flask
import os
from flask import Flask, render_template, request, jsonify
import requests
app = Flask(__name__)
GITHUB_API_URL = "https://api.github.com/search/code"
@app.route("/")
def root_route():
return render_template('search.html')
@app.route("/search")
def search():
query = request.args.get('q')
headers = {'Authorization': f'token {os.environ["GITHUB_TOKEN"]}'}
params = {'q': query}
response = requests.get(GITHUB_API_URL, headers=headers, params=params)
if response.status_code == 200:
return jsonify(response.json())
else:
return jsonify({'error': 'Failed to fetch data from GitHub'}), response.status_code
Created: | Last Updated:
Introduction to the GitHub Code Search Template
Welcome to the GitHub Code Search Template! This template allows you to create a web application that leverages the GitHub API to search for code across GitHub repositories. It features a sleek design and an intuitive user interface that displays search results in an organized manner. Whether you're looking to enhance your project with code search capabilities or just exploring the possibilities of the GitHub API, this template is a great starting point.
Getting Started
To begin using this template, simply 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 start customizing and deploying your application right away.
Initial Setup
Before you can start using the application, 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 perform searches.
- First, you'll need to generate a personal access token on GitHub. Go to your GitHub settings, select "Developer settings," then "Personal access tokens," and generate a new token with the appropriate scopes for searching code.
- Once you have your token, go to the Environment Secrets tab within the Lazy Builder.
- Create a new secret with the key
GITHUB_TOKEN
and paste your GitHub personal access token as the value.
Test: Deploying the App
With the environment secret set, you're ready to deploy the application. Press the "Test" button on the Lazy platform. This will initiate the deployment process and, if necessary, the Lazy CLI will prompt you for any required user input.
Using the App
Once the app is deployed, you will be provided with a dedicated server link to access the web interface. Navigate to this link to interact with the GitHub Code Search application. Here's how to use it:
- Enter a search term in the input field labeled "Enter search term..."
- Click the "Search" button to initiate the search.
- The search results will be displayed below the search bar, showing the name and path of the code files found.
- You can click on the links in the search results to view the code in a new tab.
Integrating the App
If you wish to integrate this code search functionality into another service or frontend, you can use the provided server link as the API endpoint. For example, you could make AJAX calls to this endpoint from a different web application to fetch search results and display them within that application's context.
Here's a sample AJAX request you might use in another application:
fetch('YOUR_SERVER_LINK/search?q=SEARCH_TERM')
.then(response => response.json())
.then(data => {
// Process and display the data
})
.catch(error => console.error('Error:', error));
Replace YOUR_SERVER_LINK
with the server link provided by Lazy and SEARCH_TERM
with the term you want to search for.
And that's it! You now have a fully functional GitHub Code Search application ready to be used and integrated as you see fit. Enjoy exploring the vast world of code available on GitHub!