-
gao55pan说:
用python的fastapi框架构建一套学生信息管理系统,mysql学生信息表有name,age,sex,address这四个字段,要求可以通过admin后台管理界面实现对学生信息的增删改查,要求目录结构合理,请给出每个文件的完整代码
1. 项目初始化
# 创建项目 mkdir student_info_system cd student_info_system # 创建虚拟环境 python -m venv venv # 激活虚拟环境 source venv/bin/activate # 安装依赖 pip install fastapi pydantic # 创建目录结构 mkdir app touch app/main.py touch app/__init__.py mkdir app/admin touch app/admin/__init__.py touch app/admin/routes.py # 创建数据库 # 这里以mysql为例 # 创建表 CREATE TABLE student_info ( name varchar(20) NOT NULL, age int(4) NOT NULL, sex varchar(2) NOT NULL, address varchar(50) NOT NULL ); # 插入一些测试数据 INSERT INTO student_info (name, age, sex, address) VALUES ('张三', 20, '男', '北京市朝阳区');
2. 创建Model
# app/admin/models.py from pydantic import BaseModel class StudentModel(BaseModel): name: str age: int sex: str address: str
3. 创建路由
# app/admin/routes.py from fastapi import APIRouter from app.admin.models import StudentModel router = APIRouter() @router.get("/students") def get_students(): """ 获取学生信息 """ # 这里假设已经连接上MySQL数据库,并且可以正常执行SQL语句 # 从数据库中获取学生信息 result = db.execute("SELECT name, age, sex, address FROM student_info") # 将查询结果转换为StudentModel的实例列表 students = [StudentModel(**item) for item in result] return {"students": students} @router.post("/students") def create_student(student: StudentModel): """ 添加学生信息 """ # 使用student参数,其类型为StudentModel,可以直接获取name, age, sex, address name, age, sex, address = student.name, student.age, student.sex, student.address # 执行SQL语句,将数据插入到数据库 db.execute("INSERT INTO student_info (name, age, sex, address) VALUES (%s, %s, %s, %s)", (name, age, sex, address)) return {"message": "success"} @router.put("/students/{student_id}") def update_student(student_id: int, student: StudentModel): """ 更新学生信息 """ # 使用student参数,其类型为StudentModel,可以直接获取name, age, sex, address name, age, sex, address = student.name, student.age, student.sex, student.address # 执行SQL语句,更新学生信息 db.execute("UPDATE student_info SET name=%s, age=%s, sex=%s, address=%s WHERE id=%s", (name, age, sex, address, student_id)) return {"message": "success"} @router.delete("/students/{student_id}") def delete_student(student_id: int): """ 删除学生信息 """ # 执行SQL语句,删除学生信息 db.execute("DELETE FROM student_info WHERE id=%s", student_id) return {"message": "success"}
4. 创建入口
# app/main.py from fastapi import FastAPI from app.admin.routes import router as admin_router app = FastAPI() # 添加管理员路由 app.include_router(admin_router, prefix="/admin")
5. 运行服务
# 运行服务 uvicorn app.main:app --reload
-
gao55pan说:
请用fastapi和Vue Admin实现学生信息的管理功能,可以实现增删改查功能,要求目录结构完整,并且给出详细的代码
。
Fastapi代码:
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() # 定义学生的Model class Student(BaseModel): id: str name: str age: int gender: str # 定义学生的列表 students = [ { "id": "stu_001", "name": "小明", "age": 18, "gender": "male" } ] # 根据id获取学生 @app.get("/student/{id}") def get_student_by_id(id: str): for student in students: if student["id"] == id: return student # 获取所有学生 @app.get("/students") def get_students(): return students # 添加学生 @app.post("/student") def add_student(student: Student): students.append(student.dict()) return student # 修改学生信息 @app.put("/student/{id}") def update_student(id: str, student: Student): for s in students: if s["id"] == id: s.update(student.dict()) return s # 删除学生 @app.delete("/student/{id}") def delete_student(id: str): for i, s in enumerate(students): if s["id"] == id: del students[i] return s
Vue Admin代码:
编辑 删除 取 消 确 定 export default { data() { return { students: [], dialogVisible: false, genders: [ { value: 'male', label: '男' }, { value: 'female', label: '女' } ], form: { name: '', age: '', gender: '' } } }, methods: { // 获取所有学生 getStudents() { this.$axios.get('http://127.0.0.1:8000/students').then(res => { this.students = res.data }) }, // 编辑学生信息 handleEdit(index, row) { this.dialogVisible = true this.form.name = row.name this.form.age = row.age this.form.gender = row.gender }, // 删除学生 handleDelete(index, row) { this.$axios.delete(`http://127.0.0.1:8000/student/${row.id}`).then(res => { this.students.splice(index, 1) }) }, // 保存学生 saveStudent() { this.$axios.put(`http://127.0.0.1:8000/student/${row.id}`, this.form).then(res => { this.students.splice(index, 1, res.data) }) } }, created() { this.getStudents() } }
-
Ss1110说:
C 语言编写一个if-else语句和switch语句编写程序表示90-100为优秀,80-89为良好,60-79为及格,60以下为不及格
if-else语句:
if(score>=90 && score=80 && score=60 && score<=79) printf("及格\n"); else printf("不及格\n");
switch语句:
switch(score){ case 90 ... 100: printf("优秀\n"); break; case 80 ... 89: printf("良好\n"); break; case 60 ... 79: printf("及格\n"); break; default: printf("不及格\n"); break; }
-
Ss1110说:
C 语言编写一个输入数字并用if-else语句和switch语句编写程序表示90-100为优秀,80-89为良好,60-79为及格,60以下为不及格
// 使用 if-else 语句
#include
int main()
{
int number;
printf("Please input a number: ");
scanf("%d", &number);
if (number >= 90 && number = 80 && number = 60 && number
int main()
{
int number;
printf("Please input a number: ");
scanf("%d", &number);
switch (number / 10)
{
case 10:
case 9:
printf("Excellent\n");
break;
case 8:
printf("Good\n");
break;
case 7:
case 6:
printf("Pass\n");
break;
default:
printf("Failed\n");
break;
}
return 0;
} -
gao55pan说:
请用Scrapy爬取https://www.chinaz.com/这个页面的文章内容,请给出详细的代码
-- coding: utf-8 --
import scrapy
class ChinazSpider(scrapy.Spider):
name = 'ChinazSpider'
allowed_domains = ['chinaz.com']
start_urls = ['https://www.chinaz.com/']def parse(self, response): # 获取文章链接 article_urls = response.xpath('//div[@class="listCentent"]/h3/a/@href').extract() # 获取每篇文章内容 for article_url in article_urls: yield scrapy.Request(url=article_url, callback=self.parse_detail) def parse_detail(self, response): # 获取文章标题 title = response.xpath('//div[@class="ph_news"]/h1/text()').extract_first() # 获取文章内容 content = response.xpath('//div[@class="ph_news"]/div[@class="ph_content"]/p/text()').extract() # 把标题和内容作为字典传给item item = { 'title': title, 'content': content } yield item