2

I am making an project with python and django about github profile. I already successful call user github API and it give me like a lot of repos from that user and I want to display like 8 of them and sorted by stars or forks or size of that repo. How can I do that?

Here's an image of my page: enter image description here

Here is my views.py:

def user(req, username):
    username = str.lower(username)

    # Get User Info

    with urlopen(f'https://api.github.com/users/{username}') as response:
        source = response.read()
    data = json.loads(source)

    # Get Limit Call API

    with urlopen(f'https://api.github.com/rate_limit') as response:
        source = response.read()
    limit_data = json.loads(source)

    # Get User Repo Info
    with urlopen(f'https://api.github.com/users/{username}/repos') as response:
        source = response.read()
    user_repos = json.loads(source)

    def sort_user_repo_by_stars(user_repos):
        return user_repos['stargazers_count']

    user_repos.sort(key=sort_user_repo_by_stars, reverse=True)

    created_at = data['created_at']
    created_at = datetime.datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ")
    created_at = created_at.strftime("%B %d, %Y")

    context = {
        'username': username,
        'data': data,
        'created_at': created_at,
        'limit_data': limit_data,
        'user_repos': user_repos,
    }
    return render(req, 'user.html', context)

and here is my template user.html:

<div class="repos">
    <div class="top-repo">
        <label for="top-repos" class="col-sm-3 col-form-label">Top Repos <span>by </span></label>
        <select class="custom-select bg-light text-primary" name="pick">
            <option selected="stars">stars</option>
            <option value="forks">forks</option>
            <option value="size">size</option>
        </select>
    </div>
    <div class="repo-info">
        {% for repo in user_repos %}
        <div class="card-deck">
            <div class="card shadow">
                <div class="card-body">
                    <h4 class="card-title">{{repo.name}}</h4>
                    <p class="card-text clearfix">
                        <i class="fas fa-circle"></i> {{repo.language}}
                        <i class="fas fa-star"></i> {{repo.stargazers_count}}
                        <i class="fal fa-code-branch"></i> {{repo.forks}}
                        <span class="float-right">{{repo.size}} KB</span>
                    </p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>
5
  • you also need to show the JSON on which sorting is to be done. Commented Jul 1, 2020 at 5:02
  • Sorry I don't get it ? Commented Jul 1, 2020 at 5:07
  • as I see you've already done sorting in your code so what's the problem then? Commented Jul 1, 2020 at 5:08
  • Ah yes my problem is it's showing all the object in an array and I just want to display like 8 objects on the page. How can I do that? Commented Jul 1, 2020 at 5:09
  • 2
    context = { .... 'user_repos': user_repos[:8], } Commented Jul 1, 2020 at 5:11

1 Answer 1

2

If you only want an array of 8 elements then you can use a slice operator on your list.

context = {
    "username": username,
    "data": data,
    "created_at": created_at,
    "limit_data": limit_data,
    "user_repos": user_repos[:8],
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much and one last thing I already sorted an array by repo stars but how can I make it like when i clicked to select and I choosed sorted by fork. and it will show repos that sorted by forks?
{% for repo in user_repos %} this is evaluated at the server-side so if you need to sort by some other attribute you can either do it using javascript or need to send multiple sorted lists in context and select them using javascript.
def sort_user_repo_by_stars(user_repos): return user_repos['stargazers_count'] user_repos.sort(key=sort_user_repo_by_stars, reverse=True) so this one is sorted by stars
@nathannewyen I think you should ask another question for the same to get better insights on what you want to do and what can be achieved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.