Python与前端集成:构建全栈应用

Python与前端集成:构建全栈应用

前言

大家好,我是第一程序员(名字大,人很菜)。作为一个非科班转码、正在学习Rust和Python的萌新,最近我开始学习Python与前端技术的集成。说实话,一开始我对全栈开发的概念还很模糊,但随着学习的深入,我发现Python作为后端与前端框架的结合可以构建出功能强大的全栈应用。今天我想分享一下我对Python与前端集成的学习心得,希望能给同样是非科班转码的朋友们一些参考。

一、后端API设计

1.1 使用FastAPI创建RESTful API

FastAPI是一个现代化的Python Web框架,非常适合构建RESTful API:

from fastapi import FastAPI from pydantic import BaseModel from typing import List app = FastAPI() class Item(BaseModel): id: int name: str price: float is_offer: bool = None items = [] @app.get("/") def read_root(): return {"message": "Hello, World!"} @app.get("/items/{item_id}") def read_item(item_id: int): for item in items: if item.id == item_id: return item return {"error": "Item not found"} @app.post("/items/") def create_item(item: Item): items.append(item) return item @app.put("/items/{item_id}") def update_item(item_id: int, item: Item): for i, existing_item in enumerate(items): if existing_item.id == item_id: items[i] = item return item return {"error": "Item not found"} @app.delete("/items/{item_id}") def delete_item(item_id: int): for i, item in enumerate(items): if item.id == item_id: items.pop(i) return {"message": "Item deleted"} return {"error": "Item not found"} 

1.2 使用Flask创建RESTful API

Flask是另一个流行的Python Web框架,也可以用于构建RESTful API:

from flask import Flask, request, jsonify app = Flask(__name__) items = [] @app.route('/', methods=['GET']) def read_root(): return jsonify({"message": "Hello, World!"}) @app.route('/items/<int:item_id>', methods=['GET']) def read_item(item_id): for item in items: if item['id'] == item_id: return jsonify(item) return jsonify({"error": "Item not found"}) @app.route('/items/', methods=['POST']) def create_item(): item = request.get_json() items.append(item) return jsonify(item) @app.route('/items/<int:item_id>', methods=['PUT']) def update_item(item_id): item = request.get_json() for i, existing_item in enumerate(items): if existing_item['id'] == item_id: items[i] = item return jsonify(item) return jsonify({"error": "Item not found"}) @app.route('/items/<int:item_id>', methods=['DELETE']) def delete_item(item_id): for i, item in enumerate(items): if item['id'] == item_id: items.pop(i) return jsonify({"message": "Item deleted"}) return jsonify({"error": "Item not found"}) if __name__ == '__main__': app.run(debug=True) 

二、前端框架集成

2.1 与React集成

React是一个流行的前端框架,可以与Python后端API集成:

// App.js import React, { useState, useEffect } from 'react'; function App() { const [items, setItems] = useState([]); const [newItem, setNewItem] = useState({ id: '', name: '', price: '', is_offer: false }); useEffect(() => { fetch('http://localhost:8000/items/') .then(response => response.json()) .then(data => setItems(data)); }, []); const handleSubmit = (e) => { e.preventDefault(); fetch('http://localhost:8000/items/', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(newItem), }) .then(response => response.json()) .then(data => { setItems([...items, data]); setNewItem({ id: '', name: '', price: '', is_offer: false }); }); }; return ( <div> <h1>Items</h1> <ul> {items.map(item => ( <li key={item.id}> {item.name} - ${item.price} </li> ))} </ul> <form onSubmit={handleSubmit}> <input type="text" placeholder="ID" value={newItem.id} onChange={(e) => setNewItem({...newItem, id: parseInt(e.target.value)})} /> <input type="text" placeholder="Name" value={newItem.name} onChange={(e) => setNewItem({...newItem, name: e.target.value})} /> <input type="number" placeholder="Price" value={newItem.price} onChange={(e) => setNewItem({...newItem, price: parseFloat(e.target.value)})} /> <button type="submit">Add Item</button> </form> </div> ); } export default App; 

2.2 与Vue集成

Vue是另一个流行的前端框架,也可以与Python后端API集成:

<!-- App.vue --> <template> <div> <h1>Items</h1> <ul> <li v-for="item in items" :key="item.id"> {{ item.name }} - ${{ item.price }} </li> </ul> <form @submit.prevent="handleSubmit"> <input type="text" placeholder="ID" v-model.number="newItem.id" /> <input type="text" placeholder="Name" v-model="newItem.name" /> <input type="number" placeholder="Price" v-model.number="newItem.price" /> <button type="submit">Add Item</button> </form> </div> </template> <script> export default { data() { return { items: [], newItem: { id: '', name: '', price: '', is_offer: false } }; }, mounted() { this.fetchItems(); }, methods: { fetchItems() { fetch('http://localhost:8000/items/') .then(response => response.json()) .then(data => { this.items = data; }); }, handleSubmit() { fetch('http://localhost:8000/items/', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(this.newItem), }) .then(response => response.json()) .then(data => { this.items.push(data); this.newItem = { id: '', name: '', price: '', is_offer: false }; }); } } }; </script> 

三、数据传输

3.1 JSON数据格式

JSON是前后端数据传输的标准格式:

# 后端返回JSON数据 from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): id: int name: str price: float @app.get("/item", response_model=Item) def get_item(): return {"id": 1, "name": "Item 1", "price": 10.99} 

3.2 处理CORS

跨域资源共享(CORS)是前后端集成中常见的问题:

# FastAPI处理CORS from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware app = FastAPI() # 配置CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], # 在生产环境中应该设置具体的域名 allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/") def read_root(): return {"message": "Hello, World!"} 
# Flask处理CORS from flask import Flask, jsonify from flask_cors import CORS app = Flask(__name__) CORS(app) # 允许所有跨域请求 @app.route('/') def read_root(): return jsonify({"message": "Hello, World!"}) 

四、认证与授权

4.1 JWT认证

JSON Web Token(JWT)是一种常用的认证方式:

# FastAPI中使用JWT from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from jose import JWTError, jwt from datetime import datetime, timedelta from pydantic import BaseModel app = FastAPI() # 配置 SECRET_KEY = "your-secret-key" ALGORITHM = "HS256" ACCESS_TOKEN_EXPIRE_MINUTES = 30 # 模拟用户数据库 fake_users_db = { "alice": { "username": "alice", "full_name": "Alice Smith", "email": "[email protected]", "hashed_password": "fakehashedsecret", "disabled": False, } } # 工具函数 def fake_hash_password(password: str): return "fakehashed" + password def verify_password(plain_password, hashed_password): return hashed_password == fake_hash_password(plain_password) def get_user(db, username: str): if username in db: user_dict = db[username] return user_dict def create_access_token(data: dict, expires_delta: timedelta = None): to_encode = data.copy() if expires_delta: expire = datetime.utcnow() + expires_delta else: expire = datetime.utcnow() + timedelta(minutes=15) to_encode.update({"exp": expire}) encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) return encoded_jwt # 依赖 oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") async def get_current_user(token: str = Depends(oauth2_scheme)): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) username: str = payload.get("sub") if username is None: raise credentials_exception except JWTError: raise credentials_exception user = get_user(fake_users_db, username=username) if user is None: raise credentials_exception return user # 路由 @app.post("/token") async def login(form_data: OAuth2PasswordRequestForm = Depends()): user = get_user(fake_users_db, form_data.username) if not user: raise HTTPException(status_code=400, detail="Incorrect username or password") if not verify_password(form_data.password, user["hashed_password"]): raise HTTPException(status_code=400, detail="Incorrect username or password") access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) access_token = create_access_token( data={"sub": user["username"]}, expires_delta=access_token_expires ) return {"access_token": access_token, "token_type": "bearer"} @app.get("/users/me") async def read_users_me(current_user: dict = Depends(get_current_user)): return current_user 

五、部署

5.1 部署后端

使用Docker部署Python后端:

# Dockerfile FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8000 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] 

5.2 部署前端

使用Vercel、Netlify等平台部署前端:

  • Vercel:适合部署React、Next.js应用
  • Netlify:适合部署Vue、React应用
  • GitHub Pages:适合部署静态网站

5.3 完整部署

使用Docker Compose部署前后端:

# docker-compose.yml version: '3' services: backend: build: ./backend ports: - "8000:8000" frontend: build: ./frontend ports: - "3000:3000" depends_on: - backend 

六、Python与Rust的对比

作为一个同时学习Python和Rust的转码者,我发现对比学习是一种很好的方法:

6.1 前端集成对比

  • Python:生态丰富,有FastAPI、Flask等框架
  • Rust:有Actix-web、Rocket等框架
  • 开发效率:Python开发效率高,Rust开发效率相对较低
  • 性能:Rust性能优异,Python性能相对较低

6.2 学习心得

  • Python的优势:开发效率高,生态丰富
  • Rust的优势:性能优异,内存安全
  • 相互借鉴:从Python学习快速开发,从Rust学习性能优化

七、实践项目推荐

7.1 全栈项目

  • 博客系统:使用Python作为后端,React/Vue作为前端
  • 电商系统:使用Python作为后端,React/Vue作为前端
  • 社交应用:使用Python作为后端,React/Vue作为前端
  • 数据分析平台:使用Python作为后端,React/Vue作为前端

八、学习方法和技巧

8.1 学习方法

  • 循序渐进:先学习后端API开发,再学习前端框架
  • 项目实践:通过实际项目来巩固知识
  • 文档阅读:仔细阅读框架的官方文档
  • 社区交流:加入社区,向他人学习

8.2 常见问题和解决方法

  • CORS问题:配置CORS中间件
  • 认证问题:使用JWT等认证方式
  • 部署问题:使用Docker等容器化技术
  • 性能问题:优化API设计,使用缓存

九、总结

Python与前端技术的集成可以构建出功能强大的全栈应用。作为一个非科班转码者,我深刻体会到全栈开发的重要性。

我的学习过程并不是一帆风顺的,遇到了很多困难和挫折,但通过不断地实践和学习,我逐渐掌握了Python与前端集成的各种技巧。

保持学习,保持输出。虽然现在我还是个菜鸡,但我相信只要坚持,总有一天能成为真正的「第一程序员」!

Read more

如何快速上手SVGA动画播放器:移动端Web动画的完整指南

如何快速上手SVGA动画播放器:移动端Web动画的完整指南 【免费下载链接】SVGAPlayer-Web-Lite 项目地址: https://gitcode.com/gh_mirrors/sv/SVGAPlayer-Web-Lite 在移动端Web开发中,流畅的动画效果对于提升用户体验至关重要。SVGAPlayer-Web-Lite作为一款专为移动端设计的轻量级动画播放器,以其卓越的性能表现和简单的使用方式,成为开发者的首选工具。本文将为您详细介绍如何快速上手这一强大的动画解决方案。 什么是SVGA播放器? SVGAPlayer-Web-Lite是一个专门针对移动端Web环境优化的动画播放器,它支持SVGA 2.x格式文件,能够在Android 4.4+和iOS 9+系统上稳定运行。相比传统动画方案,它具有以下核心优势: * 极致的轻量化:打包后体积小于60KB,gzip压缩后仅18KB * 出色的性能表现:采用多线程WebWorker解析和OffscreenCanvas技术 * 强大的兼容性:支持现代浏览器和移动设备 快速开始:5分钟完成第一个动画 安装步骤

美妆试妆系统:GLM-4.6V-Flash-WEB虚拟涂抹口红色号

美妆试妆系统中的视觉智能革命:基于 GLM-4.6V-Flash-WEB 的虚拟口红涂抹实现 在直播带货和线上美妆选购日益普及的今天,用户不再满足于“看图说话”式的商品展示。他们想要的是——我涂上这支口红会是什么样子? 尤其是面对琳琅满目的“豆沙色”“枫叶红”“吃土色”,仅靠文字描述或模特试色图,很难判断是否适合自己。这正是虚拟试妆技术的价值所在。 但要让AI真正理解“适合黄皮的哑光玫瑰色”并精准地“画”在你的嘴唇上,并非简单的图像滤镜叠加。它需要模型同时读懂图片和语言,还要具备对色彩趋势、肤色匹配、面部结构的空间感知能力。过去这类系统依赖多个独立模块拼接:人脸检测 + 唇部分割 + 色彩检索 + 渲染引擎,流程复杂、延迟高、泛化差。 而现在,随着智谱AI推出 GLM-4.6V-Flash-WEB 这类专为Web端优化的轻量级多模态大模型,我们终于看到了一种更简洁、更智能、也更可落地的解决方案。 从“拼凑系统”到“端到端理解”:为何传统方案走不通? 早年的虚拟试妆工具大多基于传统计算机视觉算法。

昨天一口气面了3家前端岗,结果全凉了...

我提前准备了半个月,八股文背得滚瓜烂熟,Vue响应式原理、Event Loop、浏览器缓存策略倒背如流。结果一天三场面试,场场被问懵,面完出来脑子都是嗡嗡的。 先简单交代一下我的情况:5年前端经验,主要技术栈是Vue2/3 + React,做过电商、中后台项目,自认为基础还算扎实。这次想跳槽去个大厂或者中型公司,薪资期望35k左右。结果现实给我狠狠上了一课! 第一场,某二线大厂,一面就挂了。 面试官上来没问八股,直接扔了个场景: “我们有个活动页,双11高峰期预估PV 200万+,页面里有十几个弹窗组件,每个弹窗都有独立的业务逻辑和样式。现在的问题是,首屏加载很慢,用户滚动时卡顿明显。你从工程化、组件设计、渲染优化三个角度,给一套完整的优化方案。” 我当场就有点懵。脑子里想的都是“代码分割、按需加载、虚拟滚动”这些零散的点,但串不成一个完整的方案。面试官追问“弹窗组件怎么设计能减少渲染开销”,我答得磕磕巴巴,最后他礼貌地说“先这样吧”。 第二场,

IntelliJ IDEA 打包 Web 项目 WAR 包(含 Tomcat 部署+常见问题解决)

IntelliJ IDEA 打包 Web 项目 WAR 包(含 Tomcat 部署+常见问题解决)

一、引言 对于 IntelliJ IDEA 新手来说,Web 项目 WAR 包打包常因步骤多、配置深而卡壳,且多数教程仅讲“打包”却忽略“部署验证”和“问题排查”。本文将从前置准备→核心配置→打包验证→Tomcat 部署→问题解决,带你完整走通流程,避开 90% 的常见坑。 二、前置准备:确认基础配置(避免起步就错) 在开始打包前,先检查 3 个关键前提,缺失任一环节可能导致后续操作失败: 1. 确认项目类型:打开项目结构(快捷键 Shift+Ctrl+Alt+S),在「Modules」中查看模块类型是否为「Web Application」,若不是,