|
| 1 | +# Coding with GitHub Copilot |
| 2 | + |
| 3 | +There are two main interfaces to GitHub Copilot - Chat and code completion. Code completion allows you to get suggestions in your code editor while you're coding. This can turn comments into code, and function signatures into full blocks of code. Code completion helps reduce the amount of boilerplate code and ceremony you need to type, allowing you to focus on the important aspects of what you're creating. |
| 4 | + |
| 5 | +## Scenario |
| 6 | + |
| 7 | +It's standard to work in phases when adding functionality to an application. Given that we know we want to allow users to filter the list of dogs based on breed, we'll need to add an endpoint to provide a list of all breeds. Later we'll add the rest of the functionality, but let's focus on this part for now. |
| 8 | + |
| 9 | +## Flask routes |
| 10 | + |
| 11 | +While we won't be able to provide a full overview of [routing in Flask](https://flask.palletsprojects.com/en/stable/quickstart/#routing), they are defined by using the Python decorator `@app.route`. There are a couple of parameters you can provide to `@app.route`, including the path (or URL) one would use to access the route (such as **api/breeds**), and the [HTTP method(s)](https://www.w3schools.com/tags/ref_httpmethods.asp) which can be used. |
| 12 | + |
| 13 | +## Code completion |
| 14 | + |
| 15 | +Code completion predicts the next block of code you're about to type based on the context Copilot has. For code completion, this includes the file you're currently working on and any tabs open in your IDE. |
| 16 | + |
| 17 | +Code completion is best for situations where you know what you want to do, and are more than happy to just start writing code with a bit of a helping hand along the way. Suggestions will be generated based both on the code you write (say a function definition) and comments you add to your code. |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> We don't provide specific prompts to use with Copilot, as part of the learning experience is to discover how to interact with Copilot. If you are unfamiliar with Flask or how to add routes, you can look at the routes defined above for inspiration, or ask Copilot chat for guidance! |
| 21 | +
|
| 22 | +## Create the breeds endpoint |
| 23 | + |
| 24 | +Let's build our new route in our Flask backend with the help of code completion. |
| 25 | + |
| 26 | +1. Return to your codespace, or reopen it by navigating to your repository and selecting **Code** > **Codespaces** and the name of your codespace. |
| 27 | +2. Open **server/app.py**. |
| 28 | +3. Locate the comment which reads `## HERE`, which should be at line 68. Delete the comment to ensure there isn't any confusion for Copilot, and leave you're cursor there. |
| 29 | +4. Add the route which will call the database to find all breeds, and returns a JSON array with their names and IDs. If you begin typing `@app.route` or add a comment with the requirements like `# Route to get all breeds`, you should notice italicized text generated by GitHub Copilot. |
| 30 | +5. Select <kbd>Tab</kbd> to accept the code suggestion. |
| 31 | + |
| 32 | + The code generated should look a little like this: |
| 33 | + |
| 34 | + ```python |
| 35 | + @app.route('/api/breeds', methods=['GET']) |
| 36 | + def get_breeds(): |
| 37 | + # Query all breeds |
| 38 | + breeds_query = db.session.query(Breed.id, Breed.name).all() |
| 39 | + |
| 40 | + # Convert the result to a list of dictionaries |
| 41 | + breeds_list = [ |
| 42 | + { |
| 43 | + 'id': breed.id, |
| 44 | + 'name': breed.name |
| 45 | + } |
| 46 | + for breed in breeds_query |
| 47 | + ] |
| 48 | + |
| 49 | + return jsonify(breeds_list) |
| 50 | + ``` |
| 51 | + |
| 52 | + > [!IMPORTANT] |
| 53 | + > Because LLMs are probabilistic, not deterministic, the exact code generated can vary. The above is a representative example. If your code is different, that's just fine as long as it works! |
| 54 | + |
| 55 | +6. Save **app.py** |
| 56 | + |
| 57 | + |
| 58 | +## Validate the endpoint |
| 59 | + |
| 60 | +With the code created and saved, let's quickly validate the endpoint to ensure it works. |
| 61 | + |
| 62 | +1. Navigate to [http://localhost:5100/api/breeds](http://localhost:5100/api/breeds) to validate the route. You should see JSON displayed which contains the list of breeds! |
| 63 | + |
| 64 | +## Summary and next steps |
| 65 | + |
| 66 | +You've added a new endpoint with the help of GitHub Copilot! Let's start down the path of performing more complex operations by [exploring our project](./2-explore-project.md). |
| 67 | + |
| 68 | +## Resources |
| 69 | + |
| 70 | +- [Code suggestions in your IDE with GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/getting-code-suggestions-in-your-ide-with-github-copilot) |
| 71 | +- [Code completions with GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/ai-powered-suggestions) |
| 72 | +- [Prompt crafting](https://code.visualstudio.com/docs/copilot/prompt-crafting) |
0 commit comments