|
| 1 | +# Coding with GitHub Copilot |
| 2 | + |
| 3 | +With code completions, GitHub Copilot provides suggestions in your code editor while you're coding. This can turn comments into code, generate the next line of code, and generate an entire function just from a signature. 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 | +The application uses a Flask app with SQLAlchemy as the backend API (in the [/server][server-code] folder), and an Astro app with Svelte as the frontend (in the [/client][client-code] folder). You will explore more of the project later; this exercise will focus solely on the Flask application. |
| 10 | + |
| 11 | +> [!NOTE] |
| 12 | +> As you begin making changes to the application, there is always a chance a breaking change could be created. If the page stops working, check the terminal window you used previously to start the application for any error messages. You can stop the app by using <kbd>Control</kbd>+<kbd>C</kbd>, and restart it by running `./scripts/start-app.ps1`. |
| 13 | +
|
| 14 | +## Flask routes |
| 15 | + |
| 16 | +While we won't be able to provide a full overview of [routing in Flask][flask-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)][http-methods] which can be used. |
| 17 | + |
| 18 | +## Code completion |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +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. |
| 23 | + |
| 24 | +## Create the breeds endpoint |
| 25 | + |
| 26 | +Let's build our new route in our Flask backend with the help of code completion. |
| 27 | + |
| 28 | +> [!IMPORTANT] |
| 29 | +> For this exercise, **DO NOT** copy and paste the code snippet provided, but rather type it manually. This will allow you to experience code completion as you would if you were coding back at your desk. You'll likely see you only have to type a few characters before GitHub Copilot begins suggesting the rest. |
| 30 | +
|
| 31 | +1. [] Return to Visual Studio Code. |
| 32 | +2. [] Open **server/app.py** file. |
| 33 | +3. [] Locate the comment which reads **## HERE**, which should be at line 68. |
| 34 | +4. [] Delete the comment to ensure there isn't any confusion for Copilot, and leave your cursor there. |
| 35 | +5. [] Begin adding the code to create the route to return all breeds from an endpoint of **api/breeds** by typing the following: |
| 36 | + |
| 37 | + ```python-nocopy |
| 38 | + @app.route('/api/breeds', methods=['GET']) |
| 39 | + ``` |
| 40 | +
|
| 41 | +6. [] Once you see the full function signature, select <kbd>Tab</kbd> to accept the code suggestion. |
| 42 | +
|
| 43 | +> [!Hint] |
| 44 | +> You can also use <kbd>control</kbd>+<kbd>Right Arrow</kbd> to accept a single word at a time. |
| 45 | +
|
| 46 | +7. [] If it didn't already, code completion should then suggest the remainder of the function signature; just as before select <kbd>Tab</kbd> to accept the code suggestion. |
| 47 | + |
| 48 | + The code generated should look a little like this: |
| 49 | +
|
| 50 | + ```python-nocopy |
| 51 | + @app.route('/api/breeds', methods=['GET']) |
| 52 | + def get_breeds(): |
| 53 | + # Query all breeds |
| 54 | + breeds_query = db.session.query(Breed.id, Breed.name).all() |
| 55 | + |
| 56 | + # Convert the result to a list of dictionaries |
| 57 | + breeds_list = [ |
| 58 | + { |
| 59 | + 'id': breed.id, |
| 60 | + 'name': breed.name |
| 61 | + } |
| 62 | + for breed in breeds_query |
| 63 | + ] |
| 64 | + |
| 65 | + return jsonify(breeds_list) |
| 66 | + ``` |
| 67 | +
|
| 68 | +> [!IMPORTANT] |
| 69 | +> 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! |
| 70 | +
|
| 71 | +8. [] Let's add a comment to the newly created function. To do this, place your cursor inside the function (anywhere between the lines **def get_breeds...** and **return jsonify...**). Then, press <kbd>Control</kbd>+<kbd>I</kbd> to open the editor inline chat. |
| 72 | +In the input box, type **/doc**. (You can optionally provide additional details, but it's not required). This will prompt GitHub Copilot to generate a documentation comment for the function. The suggested comment will appear inline in the code (highlighted in green). Click **Accept** to apply the comment to your code, or click **Close** to discard the suggestion. |
| 73 | +
|
| 74 | +> [!TIP] |
| 75 | +> You just used a slash command, a shortcut to streamline a task, these commands eliminate the need for verbose prompts. |
| 76 | +>  |
| 77 | +
|
| 78 | +9. [] **Save** the file. |
| 79 | +
|
| 80 | +## Validate the endpoint |
| 81 | +
|
| 82 | +With the code created and saved, let's quickly validate the endpoint to ensure it works. |
| 83 | +
|
| 84 | +1. [] Navigate to +++http://localhost:5100/api/breeds+++ on the browser to validate the new route. You should see JSON displayed which contains the list of breeds! |
| 85 | +2. [] Let's commit the changes locally, click on the Source control icon on the left sidebar |
| 86 | +3. [] Click on the **+** icon to stage **server/app.py** |
| 87 | +4. [] On the commit message box click on the icon with two stars, Copilot is going to create the commit message for you |
| 88 | +5. [] Click on the **Commit** blue button to commit locally if you are happy with the commit message, otherwise adjust the message before committing. |
| 89 | + - Don't try to push the changes (by pushing or synching), you do not have write permissions on the repository. |
| 90 | +
|
| 91 | +## Summary and next steps |
| 92 | +
|
| 93 | +You've added a new endpoint with the help of GitHub Copilot! You saw how Copilot predicted the next block of code you were likely looking for and provided the suggestion inline, helping save you the effort of typing it out manually. Let's start down the path of performing more complex operations by [exploring our project][walkthrough-next]. |
| 94 | +
|
| 95 | +## Resources |
| 96 | +
|
| 97 | +- [Code suggestions in your IDE with GitHub Copilot][copilot-suggestions] |
| 98 | +- [Code completions with GitHub Copilot in VS Code][vscode-copilot] |
| 99 | +- [Prompt crafting][prompt-crafting] |
| 100 | +
|
| 101 | +
|
| 102 | +[client-code]: /client/ |
| 103 | +[copilot-suggestions]: https://docs.github.com/en/copilot/using-github-copilot/getting-code-suggestions-in-your-ide-with-github-copilot |
| 104 | +[flask-routing]: https://flask.palletsprojects.com/en/stable/quickstart/#routing |
| 105 | +[http-methods]: https://www.w3schools.com/tags/ref_httpmethods.asp |
| 106 | +[prompt-crafting]: https://code.visualstudio.com/docs/copilot/prompt-crafting |
| 107 | +[server-code]: /server/ |
| 108 | +[vscode-copilot]: https://code.visualstudio.com/docs/copilot/ai-powered-suggestions |
0 commit comments