FastAPI + Streamlit todos앱
1
2
3
4
5
# requirements.txt
fastapi==0.115.8
uvicorn==0.34.0
streamlit==1.41.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
# Pydantic model to define the structure of a ToDo item
class ToDoItem(BaseModel):
id: int
title: str
description: str = None
# Simulating an in-memory database with a dictionary
to_do_list = {}
# CRUD Operations
# Create a new to-do item
@app.post("/todos/", response_model=ToDoItem)
def create_todo_item(todo_item: ToDoItem):
if todo_item.id in to_do_list:
raise HTTPException(status_code=400, detail="To-do item already exists")
to_do_list[todo_item.id] = todo_item
return todo_item
# Get a single to-do item by ID
@app.get("/todos/{todo_id}", response_model=ToDoItem)
def read_todo_item(todo_id: int):
if todo_id not in to_do_list:
raise HTTPException(status_code=404, detail="To-do item not found")
return to_do_list[todo_id]
# Update an existing to-do item
@app.put("/todos/{todo_id}", response_model=ToDoItem)
def update_todo_item(todo_id: int, todo_item: ToDoItem):
if todo_id not in to_do_list:
raise HTTPException(status_code=404, detail="To-do item not found")
to_do_list[todo_id] = todo_item
return todo_item
# Delete a to-do item
@app.delete("/todos/{todo_id}")
def delete_todo_item(todo_id: int):
if todo_id not in to_do_list:
raise HTTPException(status_code=404, detail="To-do item not found")
del to_do_list[todo_id]
return {"detail": "To-do item deleted"}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# app.py
import streamlit as st
import requests
BASE_URL = "http://127.0.0.1:8000"
# Streamlit app to interact with the FastAPI backend
st.title("To-Do List App")
# Create a new to-do item
st.header("Create a New To-Do Item")
todo_id = st.number_input("ID", min_value=1, step=1)
todo_title = st.text_input("Title")
todo_description = st.text_area("Description")
if st.button("Add To-Do"):
response = requests.post(
f"{BASE_URL}/todos/",
json={"id": todo_id, "title": todo_title, "description": todo_description},
)
if response.status_code == 200:
st.success("To-Do item added successfully!")
else:
st.error(f"Error: {response.json().get('detail')}")
# Fetch and display all to-do items
st.header("To-Do List")
response = requests.get(f"{BASE_URL}/todos/{todo_id}")
if response.status_code == 200:
todo = response.json()
st.write(f"ID: {todo['id']}")
st.write(f"Title: {todo['title']}")
st.write(f"Description: {todo['description']}")
else:
st.write("To-Do item not found.")
# Update a to-do item
st.header("Update To-Do Item")
update_id = st.number_input("Enter To-Do ID to Update", min_value=1, step=1)
new_title = st.text_input("New Title")
new_description = st.text_area("New Description")
if st.button("Update To-Do"):
response = requests.put(
f"{BASE_URL}/todos/{update_id}",
json={"id": update_id, "title": new_title, "description": new_description},
)
if response.status_code == 200:
st.success("To-Do item updated successfully!")
else:
st.error(f"Error: {response.json().get('detail')}")
# Delete a to-do item
st.header("Delete To-Do Item")
delete_id = st.number_input("Enter To-Do ID to Delete", min_value=1, step=1)
if st.button("Delete To-Do"):
response = requests.delete(f"{BASE_URL}/todos/{delete_id}")
if response.status_code == 200:
st.success("To-Do item deleted successfully!")
else:
st.error(f"Error: {response.json().get('detail')}")
참고 출처: https://medium.com/@obaff/building-a-website-with-python-fastapi-and-streamlit-418f48c41af2
Building a Website with Python, FastAPI, and Streamlit In this tutorial, we will walk through building a simple website using FastAPI, a modern web framework for building APIs with Python, as… In this tutorial, we will walk through building a simple website using FastAPI, a modern web framework for building APIs with Python, as…
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.