毕业论文信息管理系统:Streamlit 前端 + FastAPI 后端 + Docker Compose 部署
毕业论文信息管理系统:Streamlit + FastAPI + Docker Compose
本项目使用 Streamlit 构建用户友好界面,FastAPI 提供强大 API,并通过 Docker Compose 实现一键式部署的毕业论文信息管理系统。支持论文添加、查询、修改和删除,并与数据库同步数据。
系统架构
- 前端 (Streamlit): 提供用户界面,用户可通过界面添加、查询、修改和删除论文信息。* 后端 (FastAPI): 提供 API 接口,处理论文信息的增删改查操作,并与数据库交互。* 数据库: 存储论文信息,可使用 SQLite、PostgreSQL 等数据库。* Docker Compose: 将前端和后端容器化,并实现一键式部署。
实现步骤
-
创建 FastAPI 应用
- 使用 Pydantic 库定义论文信息模型。 * 使用列表存储所有论文信息(在实际应用中应该使用数据库)。 * 创建 API 路由,处理添加、查询、修改和删除论文信息的操作。
app = FastAPI() class Thesis(BaseModel): id: int title: str author: str 'class': str content: str theses = [] # 添加论文信息 @app.post('/theses/') def add_thesis(thesis: Thesis): theses.append(thesis) return {'message': 'Thesis added successfully'} # 获取个人论文信息 @app.get('/theses/{id}') def get_thesis(id: int): for thesis in theses: if thesis.id == id: return thesis return {'message': 'Thesis not found'} # 获取班级所有人的论文信息 @app.get('/theses/class/{class}') def get_theses_by_class('class': str): class_theses = [] for thesis in theses: if thesis.'class' == 'class': class_theses.append(thesis) return class_theses # 修改个人论文信息 @app.put('/theses/{id}') def update_thesis(id: int, thesis: Thesis): for i in range(len(theses)): if theses[i].id == id: theses[i] = thesis return {'message': 'Thesis updated successfully'} return {'message': 'Thesis not found'} # 删除个人论文信息 @app.delete('/theses/{id}') def delete_thesis(id: int): for thesis in theses: if thesis.id == id: theses.remove(thesis) return {'message': 'Thesis deleted successfully'} return {'message': 'Thesis not found'} ``` -
创建 Streamlit 应用
- 使用 Streamlit 的表单组件创建添加和修改论文的表单。 * 使用表格组件展示查询结果。 * 使用
requests库与 FastAPI 后端进行交互。
# 添加论文信息 def add_thesis(): st.subheader('Add Thesis') id = st.number_input('ID') title = st.text_input('Title') author = st.text_input('Author') 'class' = st.text_input('Class') content = st.text_area('Content') if st.button('Add'): thesis = { 'id': id, 'title': title, 'author': author, 'class': 'class', 'content': content } response = requests.post('http://localhost:8000/theses/', json=thesis) st.success(response.json()['message']) # 查询个人论文信息 def get_thesis(): st.subheader('Get Thesis') id = st.number_input('ID') if st.button('Get'): response = requests.get(f'http://localhost:8000/theses/{id}') thesis = response.json() if 'message' in thesis: st.error(thesis['message']) else: st.write(thesis) # 查询班级所有人的论文信息 def get_theses_by_class(): st.subheader('Get Theses by Class') class_name = st.text_input('Class') if st.button('Get'): response = requests.get(f'http://localhost:8000/theses/class/{class_name}') theses = response.json() if len(theses) == 0: st.warning('No theses found') else: st.table(theses) # 修改个人论文信息 def update_thesis(): st.subheader('Update Thesis') id = st.number_input('ID') title = st.text_input('Title') author = st.text_input('Author') 'class' = st.text_input('Class') content = st.text_area('Content') if st.button('Update'): thesis = { 'id': id, 'title': title, 'author': author, 'class': 'class', 'content': content } response = requests.put(f'http://localhost:8000/theses/{id}', json=thesis) st.success(response.json()['message']) # 删除个人论文信息 def delete_thesis(): st.subheader('Delete Thesis') id = st.number_input('ID') if st.button('Delete'): response = requests.delete(f'http://localhost:8000/theses/{id}') st.success(response.json()['message']) # 主界面 def main(): st.title('Thesis Management System') st.sidebar.title('Menu') menu = st.sidebar.radio('Select an option', ('Add Thesis', 'Get Thesis', 'Get Theses by Class', 'Update Thesis', 'Delete Thesis')) if menu == 'Add Thesis': add_thesis() elif menu == 'Get Thesis': get_thesis() elif menu == 'Get Theses by Class': get_theses_by_class() elif menu == 'Update Thesis': update_thesis() elif menu == 'Delete Thesis': delete_thesis() if __name__ == '__main__': main() ``` - 使用 Streamlit 的表单组件创建添加和修改论文的表单。 * 使用表格组件展示查询结果。 * 使用
-
使用 Docker Compose 容器化部署
- 创建
docker-compose.yml文件。 * 创建backend和frontend文件夹,分别用于存放后端和前端代码。 * 在每个文件夹中创建Dockerfile,定义容器的构建步骤。 * 在frontend文件夹中创建main.py文件 (Streamlit 应用代码) 和requirements.txt文件 (依赖库列表)。
docker-compose.yml
yaml version: '3' services: backend: build: backend ports: - '8000:8000' frontend: build: frontend ports: - '8501:8501'backend/Dockerfile
COPY ./app /app ``` **frontend/Dockerfile** ```dockerfile FROM python:3.8 COPY ./app /app COPY requirements.txt /app WORKDIR /app RUN pip install -r requirements.txt EXPOSE 8501 CMD ['streamlit', 'run', 'main.py'] ``` - 创建
-
运行和停止应用
- 运行
docker-compose up启动应用。 * 运行docker-compose down停止应用。
- 运行
总结
本项目使用 Streamlit、FastAPI 和 Docker Compose 构建了一个功能齐全的毕业论文信息管理系统,实现了用户界面、API 接口和容器化部署。您可以在此基础上进行扩展,例如添加数据库连接、用户认证等功能,以满足您的实际需
原文地址: https://www.cveoy.top/t/topic/cirM 著作权归作者所有。请勿转载和采集!