Getting Started with FastAPI
Creating Your First FastAPI Application
An API stands for Application Programming Interface. It allows different software systems to communicate with each other by transferring data in a structured way. Think of it as a bridge that lets one program access features or data from another without needing to understand its internal workings.
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def home():
return {"message": "Welcome to Incredible Data Scientist!"}
Running Your FastAPI Application
To run your FastAPI application, you can either use the built-in development server provided by the FastAPI CLI or use an ASGI server like Uvicorn, which is the recommended approach for both development and production environments.
1. Run FastAPI via CLI:
Run FastAPI via CLI to launch the app using FastAPI's development server, which automatically reloads the server on file changes for efficient development. This method is ideal for quick testing and development.
# Run FastAPI app using FastAPI's dev server
fastapi dev main.py
2. Run FastAPI via Uvicorn (recommended):
Run FastAPI via Uvicorn (recommended) for optimal performance and auto-reloading. This method ensures proper handling of changes during development.
# uvicorn: ASGI server to run FastAPI
# main: Python file name (main.py without .py)
# app: FastAPI instance (app = FastAPI())
# --reload: Auto-reload server on code changes (dev mode)
uvicorn main:app --reload
# Expose FastAPI on local network via port 80
uvicorn main:app --host 0.0.0.0 --port 80
# Run FastAPI with auto-reload on local network via port 80
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
3. Run FastAPI via __main__ Block
To run the FastAPI app via the __main__ block, you must execute the Python file directly (e.g., python main.py). This ensures the app starts only when the file is run as the main program.
# main.py
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=80)
Accessing FastAPI Server
You can access the FastAPI app in your browser using the following:
Server URL: http://127.0.0.1:8000, for the app.
Swagger UI: http://127.0.0.1:8000/docs, for API documentation.
ReDoc UI: http://127.0.0.1:8000/redoc, for Alternative API documentation.
Pylance Warning: Select the Correct Python Interpreter
If you see the warning "FastAPI could not be resolved" follow these steps:
Step-1: Press Ctrl+Shift+P to open the command palette.
Step-2: Type and select "Python: Select Interpreter".
Step-3: If your virtual environment (venv) is listed, select it. If not, click "Enter Interpreter Path", then "Find (Browse for Python)".
Step-4: Navigate to your virtual environment folder and select Scripts/python.exe (Windows) or bin/python.exe (macOS/Linux).
Exercise
- Fill in the Blanks:
- - Django is a __________ written in Python that simplifies web development.
- - Django follows the __________ architecture, which stands for __________.
- - To install Django, the package manager used is __________.
- - You can check the installed Django version using the command __________.
- - MVC stands for __________.