Initializing the Flask App for Your Word Prediction Game
Introduction: Why We Need to Initialize the Flask App
Welcome back! So far, you have learned how to score word guesses using semantic similarity, how to create a leaderboard to make your word prediction game more engaging and how to setup Flask routes. Now, it’s time to bring everything together by initializing the Flask app. This step is essential because it sets up the main structure of your web application, connects all the features you’ve built, and prepares your game to run as a real web service.
By the end of this lesson, you’ll understand how to set up the Flask app, configure it to use a database, and organize your code so that your game can grow with new features in the future.
Creating the Flask Application Instance
The first step in any Flask project is to create the Flask application instance. This object is the core of your web app — it handles requests, manages configuration, and ties everything together.
Let’s start by importing Flask and creating the app instance:
Here’s what’s happening:
from flask import Flaskimports the Flask class from the Flask library.app = Flask(__name__)creates a new Flask app. The__name__variable tells Flask where to look for resources like templates and static files.
This app object will be used to add routes, configure settings, and more.
Configuring the App for Database Use
Next, you need to set up your app to use a database. In this project, you’re using SQLite with SQLAlchemy, which is a popular way to manage data in Flask apps.
Let’s add the configuration settings:
Here’s what these lines do:
SQLALCHEMY_DATABASE_URItells Flask where your database is located. In this case, it’s a file calledhighscores.dbin your project folder.SQLALCHEMY_TRACK_MODIFICATIONSis set toFalse. Flask-SQLAlchemy will track every change made to the objects and emit signals, which can be useful for some advanced features. However, this tracking uses extra memory and processing power. For most projects, including this one, you don’t need this feature, so it’s set toFalseto save resources and make your app run more efficiently.
With these settings, your app is ready to connect to the database and store player scores.
