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 a modern web framework that is fast, efficient, and easy to use. This tool is designed for building APIs and web applications quickly, making it a great choice for projects that need to scale and remain maintainable.
By the end of this lesson, you will have a working project that is connected to a database and ready to serve static files and handle cross-origin requests. 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.
Before we dive in, let’s look at how your project will be organized. A typical project using this tool 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 database configuration.
For example, your project might look like this:
main.pyis where your application starts.- The
app/folder keeps your code organized by separating routes and database setup. - The
db/folder insideapp/contains files related to database configuration.
This structure helps keep your code clean and easy to manage as your project grows.
FastAPI is chosen for this project because it offers several advantages:
- Async support: FastAPI is built on top of modern Python async features, allowing for high performance and efficient handling of many simultaneous requests.
- Automatic OpenAPI/docs generation: FastAPI automatically generates interactive API documentation (using Swagger UI and ReDoc) based on your code, making it easy to test and understand your API.
- Pydantic validation: FastAPI uses Pydantic for data validation and serialization, ensuring that incoming and outgoing data matches your defined models and reducing boilerplate code.
However, there are some trade-offs compared to alternatives like Django or Flask:
- Smaller ecosystem: FastAPI’s ecosystem is newer and smaller than Django’s or Flask’s, so there may be fewer third-party plugins or community resources available.
- Learning curve with async: While async support is powerful, it can introduce a learning curve if you are new to asynchronous programming in Python. Some libraries may not be fully compatible with async code, requiring extra care when integrating them.
Before you start coding, you need to install the required dependencies. Open your terminal and run:
fastapiis the main web framework.uvicornis the ASGI (Asynchronous Server Gateway Interface) server used to run your app.sqlalchemyis the ORM (Object-relational mapping) for database interactions.
Note: In the CodeSignal environment, these packages are already installed for you. You can skip the installation step.
Let’s start by creating a basic application. In your main.py file, you will need to import the main application class, set up the database, configure static file serving, and enable CORS (Cross-Origin Resource Sharing).
Here is how you can set up your application:
Here’s what’s happening:
- The application is created and configured.
- The database tables are created if they do not exist.
- Static files are served from the
staticdirectory. - CORS is enabled to allow requests from any origin.
- The main router is included under the
/apiprefix.
Now, let’s connect the database to your application. The database configuration is handled in a separate file, such as app/db/session.py. This file sets up the connection to the database and prepares the base class for your models.
Here is an example of how to set up the database session:
DATABASE_URLspecifies the location of your database. Here, it will create a file calledrecipes.db.engineis the main interface to the database.SessionLocalis used to create database sessions.Baseis the base class for your database models.
In your main application file, calling Base.metadata.create_all(bind=engine) will create all tables defined in your models if they do not already exist.
As your app grows, you’ll want to keep your code organized. Routers help you split your routes into different files. This makes your code easier to read and maintain.
Suppose you have a router defined in app/routes.py. You can include it in your application like this:
from app.routes import routerimports your router.app.include_router(router, prefix="/api")tells the application to use the routes defined in your router under the/apipath.
This keeps your main application file clean and lets you organize your routes by topic or feature.
To run your application, you can use a command-line tool that serves your app. For example, you can use the following command:
uvicornis the server that runs your application.cooking_helper.main:apptells the server where to find your application instance.--host 0.0.0.0makes the app accessible from any network interface.--port 3000sets the port number.--reloadenables automatic reloading when you make changes to your code.
Sample output when you run the app:
You can now access your app in your browser or use tools like curl or Postman to interact with your API.
In this lesson, you learned how to:
- Set up a basic application in
main.py - Configure your app to use a SQLite database
- Connect the database and create your tables
- Organize your routes using routers
- Run your app using a modern server
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 project with a working database. This setup will support all the features you’ll build in the rest of the course!
