Skip to main content
In this tutorial, you’ll learn how to leverage GitHub Copilot’s inline suggestions to speed up building a Flask API. We’ll cover:
  • Inline completions for new routes
  • Inline chat refinements
  • Comment-driven code generation
  • Next-edit suggestions for refactoring

Prerequisites

  • Python ≥ 3.7
  • Flask installed (pip install flask)
  • GitHub Copilot extension enabled in your editor
  • Basic knowledge of RESTful APIs
This demo uses an in-memory database (items_db). For production workloads, integrate a persistent data store like PostgreSQL or MongoDB.

1. Basic Flask App with In-Memory DB

Start with a simple Flask application. Create an app.py file:
As you type, Copilot suggests method bodies in real time.

2. Deleting All Items

Type a new route decorator and function signature:
Copilot may auto-complete:

3. Generating Random Items

Define a “create random items” endpoint:
Copilot often suggests:

4. Inline Chat: Analyze Numbers

Start with:
Invoke Copilot’s inline chat and ask for number analysis. It may return:

4.1 Enhanced Validation & Stats

Refine the route for stronger validation:

5. Comment-Driven Suggestions

Write a descriptive comment, then let Copilot generate code:

6. Next-Edit Suggestions for Refactoring

Enable Next-Edit Suggestions in Copilot settings to receive automated refactors. Example: renaming items_db to items_db_new. Before:
After accepting suggestion:

Enabling Next-Edit Suggestions

Open GitHub Copilot settings and search for next edit suggestions. Toggle it on under the Preview options:
The image shows a settings interface for GitHub Copilot in a code editor, displaying options for enabling auto completions and configuring language-specific settings.

Comparison of Copilot Features

By combining these Copilot features, you can write, refine, and refactor Flask APIs faster and with confidence.

Watch Video