For this assignment you are to create a Kivy app that uses the GitHub API to lookup and display the names of a GitHub user’s public repositories. I have given you quite a few details in these instructions which will help you build your solution, so be sure to read the instructions carefully a couple times before beginning.
The URL to get a user’s repositories is as follows:
https://api.github.com/users/<username>/repos
Replace <username>
with the name of the user you are looking up. By default at most 30 repositories will be returned. This can be increased up to 100 by setting the per_page
parameter in the URL. Here is a URL to get up to 100 repositories from the user mozilla
:
https://api.github.com/users/mozilla/repos?per_page=100
If the user has more than 100 repositories it is possible to fetch multiple pages of repositories, but do not worry about that for this assignment. See the github_api.py
file from the web API in-class examples repository for an example of how to use Python to fetch the JSON from this URL and get the repository names. In addition to Kivy, your virtual environment for this assignment will need to have the requests
module installed.
The in-class example does not handle the case where the user does not exist. In that case the response will have a status code of 404, indicating the URL was not found on the server. If the status code was 200, everything went OK. You can check the response’s status code by examining response.status_code
like this:
if response.status_code == 200:
# the response was OK and you can use response.content
else:
# the request did not go through OK, so either the user was not found or
# the URL was not well formed
Your Kivy app must have a text input area where the user can type a GitHub username.
There must be a button that the user presses to look up the user’s repositories.
There must be an area where the names of the user’s public repositories are displayed after pressing the button. If the list of repositories is long enough that it does not fit in the display area, you must be able to scroll through the list. Use the ScrollView
widget to accomplish this.
If the user does not exist, display a “user does not exist” message in the display area.
Here is a screenshot of my solution after entering mozilla for the user and pressing the submit button:
Note that this layout is very similar to what I asked you to create for the Kv Language assignment. I made some adjustments to the sizes of the different widgets using size hints, which can be used to specify relative sizes. Here is the Kv file that I used:
<GithubLayout>
cols: 1
ScrollView:
size_hint_y: 20
Label:
id: output
size_hint: None, None
size: self.texture_size
TextInput:
id: input
size_hint_y: 1
hint_text: 'Enter a GitHub username here'
Button:
id: button
size_hint_y: 1
text: 'Submit'
on_release: root.process_input()
Note that the output is displayed in a Label
that is within a ScrollView
. This allows the user to scroll the Label
if there is more text than can fit on the screen. The lines size_hint: None, None
and size: self.texture_size
within the Label
are important so that the width and height of the Label
are not constrained, allowing it to be bigger than the ScrollView
, which is what allows for scrolling.
GithubLayout
is a class that extends GridView
and has the process_input()
method. If you want to use this same Kv file you will need to create the GithubLayout
class yourself, which will look like this:
class GithubLayout(GridLayout):
def process_input(self):
# Here is where you need to get the data from the API
You’ll need to import GridLayout
like this:
from kivy.uix.gridlayout import GridLayout`
Your App
class’s build()
method should return an instance of GithubLayout
so that it is the root widget.
Within the process_input()
method you’ll want to get the username from the TextInput
widget. Since that widget has the ID input
you can get the text using self.ids.input.text
. Once you get the response you’ll want to build a string containing all of the repository names separated by newlines ('\n'
between each repo name). Then you can assign that string to self.ids.output.text
to set the text of the Label
and display the list of repositories.
Push your code to git-keeper. As always, follow PEP 8.