Introduction: Getting Started with Your Cooking App

Welcome to your first step in building the AI Cooking Helper! In this lesson, you will learn how to set up the foundation of your web application using Flask, a popular Python web framework. Flask is lightweight, easy to use, and perfect for beginners who want to build web apps quickly.

By the end of this lesson, you will have a working Flask project that is connected to a database. This setup is essential for storing recipes, user data, and more as you continue to develop your cooking app. Everything you learn here will be used in future lessons as you add more features and functionality.

Python Project Structure Basics

Before we dive in, let’s quickly remind ourselves how Python projects are usually organized. A typical Python project has a main file (like main.py) that starts the application, and folders (like app/) that hold different parts of the code, such as routes and models.

For example, your project might look like this:

  • main.py is where your application starts.
  • The app/ folder keeps your code organized by separating routes (URLs) and models (database structure).

This structure helps keep your code clean and easy to manage as your project grows.

Using a Virtual Environment

Before you start installing packages for your project, it’s a best practice to use a virtual environment. A virtual environment is an isolated space on your computer where you can install Python packages just for your project, without affecting other projects or system-wide packages. This helps prevent dependency conflicts and makes it easier to manage your project’s requirements.

To create and activate a virtual environment, use the following commands in your project directory:

On macOS/Linux:

On Windows:

  • python3 -m venv venv (or python -m venv venv on Windows) creates a new virtual environment in a folder named venv.
  • source venv/bin/activate (or venv\Scripts\activate on Windows) activates the virtual environment.

Once activated, any packages you install (like Flask or SQLAlchemy) will only be available inside this environment. When you’re done working, you can deactivate the environment by running deactivate.

Note: In some online coding environments (like CodeSignal), you may not need to set up a virtual environment because the environment is already managed for you. However, for your own projects on your computer, always use a virtual environment to keep things organized and avoid conflicts.

Setting Up a Flask Application

Let’s start by creating a basic Flask application. In your main.py file, you will need to import Flask and create an app instance.

Here’s what’s happening:

  • from flask import Flask imports the Flask class.
  • app = Flask(__name__) creates a new Flask application. The __name__ variable is used by Flask to determine the root path of the application for locating templates, static files, etc.

Next, you need to configure your app to use a database. For this project, we’ll use SQLite, which is a simple file-based database that works well for small projects.

Add the following configuration to your create_app function:

  • SQLALCHEMY_DATABASE_URI tells Flask where your database is located. Here, it will create a file called recipes.db.
Connecting SQLAlchemy and Creating the Database

Now, let’s connect SQLAlchemy to your Flask app. SQLAlchemy is a library that helps you work with databases using Python code instead of SQL commands.

First, you need to import your database object that will hold the connection between your database and the Flask application. In this project, it’s defined in app/models.py as db. You also need to initialize it with your Flask app.

  • db.init_app(app) connects your database to the Flask app.

To make sure your database tables are created, you need to call db.create_all() inside the app context. This tells SQLAlchemy to create any tables you have defined in your models.

  • with app.app_context(): sets up the application context, which is needed for database operations.
  • db.create_all() creates all tables defined in your models if they don’t already exist. For production applications with evolving schemas, migration tools like are typically used, but is suitable for our initial setup and development.
Registering Blueprints for Clean Code Organization

As your app grows, you’ll want to keep your code organized. Flask blueprints help you split your routes into different files. This makes your code easier to read and maintain.

As you will define and learn how to do this in future lessons, for now let's suppose you have a routes blueprint defined in app/routes.py. You can register it with your app like this:

  • from app.routes import routes imports your blueprint.
  • app.register_blueprint(routes) tells Flask to use the routes defined in your blueprint.

This keeps your main application file clean and lets you organize your routes by topic or feature.

Running the App and Using the Flask Shell

To use flask, you first need to set the FLASK_APP environment variable:

This will tell Flask where to look for the creation of the app, in our case inside the cooking_helper/main.py file, running the create_app function.

Now that your app and environment is set up, you can run it and interact with it. To start your Flask app, add the following code at the bottom of your main.py file:

  • This checks if the file is being run directly and then starts the app.
  • host="0.0.0.0" makes the app accessible from any network interface.
  • port=3000 sets the port number.
  • debug=True enables debug mode, which helps you see errors and automatically reloads the app when you make changes.

Sample output when you run the app:

You can also use flask run to execute the app.

To interact with your app and database, you can also use the Flask shell by running:

Summary and What’s Next

In this lesson, you learned how to:

  • Set up a basic Flask application in main.py
  • Configure your app to use a SQLite database
  • Connect SQLAlchemy and create your database tables
  • Register blueprints to keep your code organized
  • Run your app and use the Flask shell for database commands

As your projects grow, it’s important to keep sensitive information—like your database URI and other configuration values—secure. In real-world applications, you should use environment variables or a separate configuration file (not committed to version control) to store these settings, rather than hard-coding them in your source code. This helps protect your data and makes your app easier to configure for different environments.

You now have a solid foundation for your AI Cooking Helper. In the next practice exercises, you’ll get hands-on experience with these steps, making sure you can set up and run your own Flask project with a working database. This setup will support all the features you’ll build in the rest of the course!

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