In this blog series, readers will learn how to build a To-Do App with FastAPI. The series will guide you step by step in creating a To-Do application from scratch using FastAPI, a modern and high-performance Python web framework. The content covers everything from setting up the development environment to deploying the app. By the end of the series, readers will have created their own To-Do App and gained a solid grasp of FastAPI.
Each post in the series will focus on a specific aspect of the development process, providing clear explanations and practical code examples. The goal is to equip readers with the knowledge and skills needed to build web applications using FastAPI. Whether you're a beginner looking to learn web development or an experienced developer exploring FastAPI, this series aims to provide valuable insights and hands-on experience.
The initial post will introduce FastAPI to help you set up your development environment and create your FastAPI application.
Blog Code in Todo_Part1 dir: GitHub - jamesbmour/blog_tutorials
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints. It's designed to be easy to use, fast to code, ready for production, and based on the latest Python features.
Compared to other popular Python web frameworks like Flask or Django, FastAPI offers:
For our Todo App, FastAPI is an excellent choice because:
Make sure you have Python 3.7 or later installed. You can download it from python.org.
It's best practice to use a virtual environment for Python projects. Here's how to create one:
python -m venv todo-env source todo-env/bin/activate # On Windows, use `todo-env\Scripts\activate`
conda create --name todo-env python=3.9 conda activate todo-env
poetry install # activate the virtual environment poetry shell
Now, let's install FastAPI and Uvicorn (an ASGI server):
pip install fastapi uvicorn
Create a new file named main.py and add the following code:
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello, FastAPI!"}
To run the application, use the following command:
uvicorn main:app --reload
This command tells Uvicorn to:
Once your server is running, you can access the automatic API documentation by navigating to http://127.0.0.1:8000/docs in your web browser.
Create a new directory for your project:
mkdir fastapi-todo-app cd fastapi-todo-app
Create the following files in your project directory:
Add the following to your requirements.txt:
fastapi uvicorn
And add this to your .gitignore:
__pycache__ *.pyc todo-env/
We've already created a root endpoint in our main.py. Let's modify it slightly:
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Welcome to the Todo App!"}
FastAPI automatically converts the dictionary we return into a JSON response.
Run your server with uvicorn main:app --reload, then navigate to http://127.0.0.1:8000 in your web browser. You should see the JSON response.
Navigate to http://127.0.0.1:8000/docs to see the Swagger UI documentation for your API. You can test your endpoint directly from this interface.
In the upcoming blog post, we will explore FastAPI in more detail by developing the fundamental features of our Todo App. We will establish endpoints for adding, retrieving, updating, and deleting todos. As an exercise, you can add a new endpoint that provides the current date and time when accessed.
@app.get("/current-time") async def get_current_time(): current_time = datetime.now() return { "current_date": current_time.strftime("%Y-%m-%d"), "current_time": current_time.strftime("%H:%M:%S"), "timezone": current_time.astimezone().tzname() }
Congratulations! You've successfully set up your development environment, created your first FastAPI application, and learned about the basic structure of a FastAPI project.
We've covered a lot of ground in this post. We've discussed what FastAPI is and why it's a great choice for our Todo App. We've also written our first endpoint and run our application.
In the next post, we'll start building the core functionality of our Todo App. Stay tuned, and happy coding!
If you would like to support me or buy me a beer feel free to join my Patreon jamesbmour
The above is the detailed content of FastAPI Todo App: Setting Up Your Todo App Project. For more information, please follow other related articles on the PHP Chinese website!