Initializing a Flask App and Setting Up the Database
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.pyis 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(orpython -m venv venvon Windows) creates a new virtual environment in a folder namedvenv.source venv/bin/activate(orvenv\Scripts\activateon 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.
