Lesson 2
Code-Based Server Initialization
Code-Based Server Initialization in FastAPI

Hello, and welcome back! In the previous lesson, we explored how to run a FastAPI application using Uvicorn from the terminal. Today, we'll take a step forward and learn how to configure and run the FastAPI server directly through a Python script.

This approach offers a more integrated way to start your server, especially when your application grows in complexity. By the end of this lesson, you'll be able to run your FastAPI application simply by executing your Python script.

Importing Necessary Modules

In this unit, you’ll need two essential modules: FastAPI and uvicorn. Why do we need these?

FastAPI allows you to define your web API, and uvicorn helps you run it.

Here’s the code to import both:

Python
1# Import FastAPI class 2from fastapi import FastAPI 3# Import uvicorn for running the server 4import uvicorn

Let's move on to create our server initialization with uvicorn.

Adding Uvicorn Server Initialization Code

Once we imported our modules, we need to define our FastAPI application instance and add our Uvicorn server initialization code.

In Python scripts, the if __name__ == "__main__": block is used to ensure that certain parts of the code are only executed when the script is run directly, and not when it is imported as a module in another script.

Within this block, we use uvicorn.run() to start the FastAPI server. Here’s the complete code snippet:

Python
1# Create an instance of the FastAPI class 2app = FastAPI() 3 4# Check if the script is executed directly 5if __name__ == "__main__": 6 # Run the app with uvicorn 7 uvicorn.run(app, host="127.0.0.1", port=8000)
  • app: The FastAPI instance to run.
  • host: The host IP where the server will be accessible.
    • Using "127.0.0.1" restricts access to the local machine.
    • Using "0.0.0.0" makes it accessible externally.
  • port: The port on which the server will run, in this case, 8000.
Running the Application with Python

Now that our script is ready, we can run the application using Python. Let’s gather all the code snippets we’ve covered so far into one complete script.

Python
1from fastapi import FastAPI 2import uvicorn 3 4app = FastAPI() 5 6if __name__ == "__main__": 7 uvicorn.run(app, host="127.0.0.1", port=8000)

To run this script, you can use a simple Python command with the name of your file, such as:

Bash
1python filename.py

By running this command, you start the FastAPI server on http://127.0.0.1:8000. You will see logs in the console indicating that the server is running and listening on http://127.0.0.1:8000. This means your FastAPI application is now accessible and ready to receive HTTP requests.

Lesson Summary

In this lesson, you learned how to set up and run a FastAPI server directly through a Python script.

We covered:

  • Importing necessary modules: FastAPI and uvicorn.
  • Adding Uvicorn server initialization code with the if __name__ == "__main__": block.
  • Running the application with a simple Python command.

Now it's time to practice! Understanding how to run a FastAPI server through a script will significantly improve your ability to manage more complex applications seamlessly.

Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.