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 Flask imports 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_URI tells Flask where your database is located. In this case, it’s a file called highscores.db in your project folder.
  • SQLALCHEMY_TRACK_MODIFICATIONS is set to False. 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 to False to 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.

Registering Blueprints and Initializing the Database

Now, let’s organize your code and set up the database. In Flask, blueprints help you keep your routes organized, especially as your app grows. You also need to initialize the database so it’s ready to store data.

Here’s how you do it:

Let’s break this down:

  • from app.routes import routes imports your routes (the different pages or actions in your app).
  • from app.models import db imports your database object.
  • db.init_app(app) connects your database to the Flask app.
  • app.register_blueprint(routes) adds your routes to the app, so Flask knows how to handle different URLs.
  • with app.app_context(): db.create_all() creates the database tables if they don’t exist yet. The app.app_context() part is needed so Flask knows which app you’re working with.

This setup keeps your code organized and makes it easy to add new features later.

Running the Flask App

The final step is to run your Flask app so you can see it in action. Here’s how you do it:

Here’s what’s happening:

  • if __name__ == "__main__": checks if you’re running this file directly (not importing it from somewhere else).
  • app = create_app() creates your Flask app using the function you just built.
  • app.run(host="0.0.0.0", port=3000, debug=True) starts the web server. The app will be available on port 3000, and debug=True helps you see errors and automatically reloads the app when you make changes.

Expected output:

When you run this code, you should see something like:

This means your app is running and ready to handle requests!

Summary And What’s Next

In this lesson, you learned how to initialize your Flask app, configure it for database use, organize your routes with blueprints, and start the server. These steps bring together everything you’ve built so far and set the stage for adding even more features.

Next, you’ll get a chance to practice these steps yourself. You’ll see how each part fits together and get hands-on experience running your own Flask app. This foundation will make it much easier to add new game features and improvements in the future. Keep going — you’re making great progress!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal