Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-17b-128e-instruct· MetaHugging Face LMArena Human Preference 140K

give me fastapi todo app

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Mistralmistral-medium-2505’s answer.
  1. User

    give me fastapi todo app

  2. Meta Assistant

    Below is a simple example of a Todo List application using FastAPI. This application will allow you to create, read, update, and delete (CRUD) todo items.

    First, ensure you have FastAPI and Uvicorn installed. If not, you can install them using pip:

    pip install fastapi uvicorn
    

    Here's the FastAPI Todo App code:

    from fastapi import FastAPI, HTTPException
    from pydantic import BaseModel
    
    app = FastAPI()
    
    # In-memory data store for simplicity
    # In a real application, you'd use a database
    todo_list = [
        {"id": 1, "title": "Buy groceries", "completed": False},
        {"id": 2, "title": "Walk the dog", "completed": True},
    ]
    
    class TodoItem(BaseModel):
        title: str
        completed: bool = False
    
    class TodoItemUpdate(BaseModel):
        title: str | None = None
        completed: bool | None = None
    
    # Get all todo items
    @app.get("/todos/")
    async def read_all_todos():
        return todo_list
    
    # Get a single todo item by ID
    @app.get("/todos/{todo_id}")
    async def read_todo(todo_id: int):
        for todo in todo_list:
            if todo["id"] == todo_id:
                return todo
        raise HTTPException(status_code=404, detail="Todo not found")
    
    # Create a new todo item
    @app.post("/todos/")
    async def create_todo(todo: TodoItem):
        new_id = max(todo["id"] for todo in todo_list) + 1 if todo_list else 1
        new_todo = {"id": new_id, "title": todo.title, "completed": todo.completed}
        todo_list.append(new_todo)
        return new_todo
    
    # Update a todo item
    @app.put("/todos/{todo_id}")
    async def update_todo(todo_id: int, todo: TodoItemUpdate):
        for existing_todo in todo_list:
            if existing_todo["id"] == todo_id:
                if todo.title is not None:
                    existing_todo["title"] = todo.title
                if todo.completed is not None:
                    existing_todo["completed"] = todo.completed
                return existing_todo
        raise HTTPException(status_code=404, detail="Todo not found")
    
    # Delete a todo item
    @app.delete("/todos/{todo_id}")
    async def delete_todo(todo_id: int):
        for todo in todo_list:
            if todo["id"] == todo_id:
                todo_list.remove(todo)
                return {"message": f"Todo {todo_id} deleted"}
        raise HTTPException(status_code=404, detail="Todo not found")
    

    To run this application, save the code in a file named main.py and execute it using Uvicorn:

    uvicorn main:app --reload
    

    You can now interact with your Todo List API at http://127.0.0.1:8000/docs using the automatically generated Swagger UI by FastAPI.

    API Endpoints

    • GET /todos/: List all todo items.
    • GET /todos/{todo_id}: Get a specific todo item.
    • POST /todos/: Create a new todo item.
    • PUT /todos/{todo_id}: Update a todo item.
    • DELETE /todos/{todo_id}: Delete a todo item.

    Example Usage

    1. List all todos: curl http://127.0.0.1:8000/todos/
    2. Create a new todo: curl -X POST -H "Content-Type: application/json" -d '{"title": "New Todo", "completed": false}' http://127.0.0.1:8000/todos/
    3. Get a specific todo: curl http://127.0.0.1:8000/todos/1
    4. Update a todo: curl -X PUT -H "Content-Type: application/json" -d '{"title": "Updated Todo"}' http://127.0.0.1:8000/todos/1
    5. Delete a todo: curl -X DELETE http://127.0.0.1:8000/todos/1

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.