# backendpyfrom fastapi import FastAPIapp = FastAPIdatabase = appgetpapersdef get_papers return databaseapppostpapersdef add_paperpaper databaseappendpaper return message Paper added successf
# backend.py
from fastapi import FastAPI
app = FastAPI()
database = []
@app.get("/papers")
def get_papers():
return database
@app.post("/papers")
def add_paper(paper):
database.append(paper)
return {"message": "Paper added successfully"}
@app.put("/papers/{paper_id}")
def update_paper(paper_id, updated_paper):
database[paper_id] = updated_paper
return {"message": "Paper updated successfully"}
@app.delete("/papers/{paper_id}")
def delete_paper(paper_id):
del database[paper_id]
return {"message": "Paper deleted successfully"}
# frontend.py
import streamlit as st
import requests
BACKEND_URL = "http://localhost:8000"
def get_papers():
response = requests.get(f"{BACKEND_URL}/papers")
return response.json()
def add_paper(paper):
response = requests.post(f"{BACKEND_URL}/papers", json=paper)
return response.json()
def update_paper(paper_id, updated_paper):
response = requests.put(f"{BACKEND_URL}/papers/{paper_id}", json=updated_paper)
return response.json()
def delete_paper(paper_id):
response = requests.delete(f"{BACKEND_URL}/papers/{paper_id}")
return response.json()
def main():
st.title("毕业论文信息管理系统")
# 添加论文
st.subheader("添加论文")
paper_title = st.text_input("论文标题")
paper_author = st.text_input("论文作者")
if st.button("添加"):
paper = {"title": paper_title, "author": paper_author}
add_paper(paper)
st.success("论文添加成功")
# 查询论文
st.subheader("查询论文")
all_papers = get_papers()
for paper in all_papers:
st.write(f"标题:{paper['title']}, 作者:{paper['author']}")
# 修改论文
st.subheader("修改论文")
paper_id = st.text_input("要修改的论文ID")
updated_title = st.text_input("修改后的标题")
updated_author = st.text_input("修改后的作者")
if st.button("修改"):
updated_paper = {"title": updated_title, "author": updated_author}
update_paper(paper_id, updated_paper)
st.success("论文修改成功")
# 删除论文
st.subheader("删除论文")
delete_id = st.text_input("要删除的论文ID")
if st.button("删除"):
delete_paper(delete_id)
st.success("论文删除成功")
if __name__ == "__main__":
main()
# docker-compose.yml
version: '3'
services:
backend:
build: .
ports:
- "8000:8000"
frontend:
build: .
ports:
- "8501:8501"
depends_on:
- backend
The backend.py file defines the backend API using the FastAPI framework. It includes endpoints for retrieving, adding, updating, and deleting papers from a database. The database variable is a list that stores the papers.
The frontend.py file is a Streamlit application that provides a user interface for interacting with the backend API. It uses the requests library to make API calls to the backend.
The docker-compose.yml file is a Docker Compose configuration file that defines two services: backend and frontend. The backend service builds an image from the current directory and exposes port 8000. The frontend service also builds an image from the current directory and exposes port 8501. The frontend service depends on the backend service.
To run the application, you can use the following commands:
docker-compose build
docker-compose up
This will build the Docker images and start the containers. The backend API will be accessible at http://localhost:8000 and the frontend application will be accessible at http://localhost:8501.
原文地址: https://www.cveoy.top/t/topic/jgHQ 著作权归作者所有。请勿转载和采集!