其他
使用 Reflex 构建全栈 Python Web 应用时…
在开发或排查 Reflex(rx)全栈 Python Web 应用、组件、状态、事件、样式、数据库、API、认证、部署或封装 React 组件时,按主题查阅官方文档并遵循核心概念。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 39/100 ×0.15
适用场景
使用 Reflex 构建全栈 Pytho特定
解决思路
- 使用 Reflex 以纯 Python 构建全栈 Web 应用,不需要 JavaScript。
- 不要依赖训练数据中的 Reflex 知识;始终优先查阅参考文档,因为训练数据可能过时。
- 需要 Getting Started 文档时,查阅 https://reflex.dev/docs/getting-started/introduction。
- 需要 Components 文档时,查阅 https://reflex.dev/docs/components/props。
- 需要 State 文档时,查阅 https://reflex.dev/docs/state/overview。
- 需要 Vars 文档时,查阅 https://reflex.dev/docs/vars/base-vars。
- 需要 Events 文档时,查阅 https://reflex.dev/docs/events/events-overview。
- 需要 Pages & Routing 文档时,查阅 https://reflex.dev/docs/pages/overview。
- 需要 Styling 文档时,查阅 https://reflex.dev/docs/styling/overview。
- 需要 Database 文档时,查阅 https://reflex.dev/docs/database/overview。
- 需要 Assets 文档时,查阅 https://reflex.dev/docs/assets/overview。
- 需要 Authentication 文档时,查阅 https://reflex.dev/docs/authentication/authentication-overview。
- 需要 Client Storage 文档时,查阅 https://reflex.dev/docs/client-storage/overview。
- 需要 API Routes 文档时,查阅 https://reflex.dev/docs/api-routes/overview。
- 需要 API Reference 文档时,查阅 https://reflex.dev/docs/api-reference/cli。
- 需要 Custom Components 文档时,查阅 https://reflex.dev/docs/custom-components/overview。
- 需要 Wrapping React 文档时,查阅 https://reflex.dev/docs/wrapping-react/overview。
- 需要 Component Library 文档时,查阅 https://reflex.dev/docs/library。
- 需要 Recipes 文档时,查阅 https://reflex.dev/docs/recipes。
- 需要 GitHub Source(docs)时,查阅 https://github.com/reflex-dev/reflex/tree/main/reflex/docs。
- 将 State(`rx.State`)定义为服务端 Python 类来保存应用状态;把 vars 定义为类属性,把事件处理器定义为方法。
- 在 State 上定义带类型的类属性作为 Base Vars,例如 `count: int = 0`。
- 使用 `@rx.var` 装饰器定义 Computed Vars 来派生计算值。
- 在组件中使用 Var Operations 转换 vars,而无需事件处理器。
- 使用 `rx.*` 组件构建所有 UI;这些组件是 React 的 Python 封装。
- 让组件接受 props,并进行组合/嵌套。
- 使用 `rx.cond()` 做条件渲染,使用 `rx.foreach()` 做迭代。
- 将 Event Handlers 定义为 State 上使用 `@rx.event` 装饰的方法。
- 使用 `on_click`、`on_change` 等组件 props 作为 Event Triggers。
- 从事件处理器返回多个事件以实现 Event Chaining。
- 使用为每个 base var 自动生成的 `set_<var>` 事件处理器作为 Setters。
- 使用 `rx.page` 装饰页面函数,或通过 `app.add_page()` 添加页面。
- 使用括号语法 `[slug]` 创建动态路由。
- 使用 inline props、Tailwind CSS、主题和响应式设计进行样式设置。
- 使用 `rx.theme()` 进行全局主题设置。
- 使用内置 SQLModel 集成来创建数据库表。
- 定义模型、运行查询并设置关系。
适用边界 · 注意事项
- 用户没有使用 Reflex(rx)、未编写导入 reflex 的 .py 文件,或问题与 Reflex 概念无关时,不要使用。
- 用户要解决的是其他 Python Web 框架(如 Django、Flask、FastAPI)或原生 React 问题时,不适用。
- 用户明确无法访问外部 URL 或要求完全离线、不使用官方文档时,URL 参考做法不适用。