Creating the Main Game Routes in Flask
Introduction: Connecting the Game with Routes
Welcome back! So far, you’ve learned how to score word guesses using semantic similarity and set up a leaderboard. Now, it’s time to connect all these features together so your game can interact with players through the web.
In this lesson, you’ll learn how to create the main routes (or endpoints) in Flask that allow your frontend to communicate with your backend. These routes will let players get the daily prompt, submit their guesses, and see the leaderboard. By the end of this lesson, you’ll understand how each route works and how they power your word prediction game.
Recall: Flask Blueprints and Project Structure
Before we dive in, let’s quickly remind ourselves about Flask blueprints and how they help organize your project.
A blueprint in Flask is a way to organize your routes and related code into reusable components. In our project, we use a blueprint to keep all the game-related routes together. This makes the code easier to manage, especially as your app grows.
For example, in your project, you might see something like this:
This creates a blueprint called routes. All the routes for your game will be registered under this blueprint, keeping things tidy and modular.
Building the /game Route
Let’s start by creating the route that sends the daily game data to the frontend. This is the /game route.
Step 1: Defining the Route
First, we define a new route using our blueprint. We want this route to respond to GET requests.
Here, @routes.route("/game", methods=["GET"]) tells Flask to call the game_data function whenever someone visits /game with a GET request.
Step 2: Getting the Daily Prompt
Inside the function, we need to get today’s prompt. This is usually a dictionary with the system prompt, user question, and the LLM model to use.
The get_daily_prompt() function (defined elsewhere in your project) returns the prompt for today’s game.
