mesop
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMesop
Mesop
Use this skill to build, debug, and deploy Mesop applications. Mesop is a Python-native UI framework that enables developers to build web apps without writing frontend code (HTML/CSS/JS).
使用此技能来构建、调试和部署Mesop应用。Mesop是一个原生Python UI框架,开发者无需编写前端代码(HTML/CSS/JS)即可构建Web应用。
Quick Triage
快速判断
- Use this skill for pure Python web UIs, especially for AI/ML demos and internal tools.
- Do NOT use this skill if the user specifically requests a different Python framework (Streamlit, Gradio, Solara) unless they are asking for a comparison or migration.
- Do NOT use this skill for general Flask/FastAPI backend development unless it's specifically about mounting a Mesop app.
- 此技能适用于纯Python Web UI场景,尤其适合AI/ML演示和内部工具。
- 若用户明确要求使用其他Python框架(Streamlit、Gradio、Solara),除非是询问对比或迁移方案,否则请勿使用此技能。
- 除非是关于挂载Mesop应用的特定需求,否则请勿将此技能用于通用Flask/FastAPI后端开发。
Workflow
工作流程
- Setup: Install and create
mesop.main.py - Define Page: Use to define the entry point.
@me.page(path="/") - Define State: Create a to hold session state (must be serializable).
@me.stateclass - Create Components: Build the UI tree using ,
me.box,me.text, etc.me.input - Handle Events: Write event handler functions (regular or generator) to update state.
- Style: Apply styles using (flexbox/grid) for layout and appearance.
me.Style - Deploy: Deploy to Cloud Run, Docker, or Hugging Face Spaces.
- 环境搭建:安装并创建
mesop文件。main.py - 定义页面:使用定义应用入口点。
@me.page(path="/") - 定义状态:创建来存储会话状态(必须可序列化)。
@me.stateclass - 创建组件:使用、
me.box、me.text等组件构建UI树。me.input - 处理事件:编写事件处理函数(常规函数或生成器)来更新状态。
- 样式设置:使用(flexbox/grid)来设置布局和外观样式。
me.Style - 部署应用:部署至Cloud Run、Docker或Hugging Face Spaces。
Essential Patterns
核心模式
Standard Import
标准导入
Always use the standard alias:
python
import mesop as me
import mesop.labs as mel # For labs components like chat始终使用标准别名:
python
import mesop as me
import mesop.labs as mel # For labs components like chatState Management
状态管理
State is session-scoped and must be serializable.
python
@me.stateclass
class State:
count: int = 0
input_value: str = "" # Immutable default
# items: list[str] = field(default_factory=list) # Mutable default patternAccessing State: inside components or event handlers.
state = me.state(State)状态是会话级别的,且必须可序列化。
python
@me.stateclass
class State:
count: int = 0
input_value: str = "" # Immutable default
# items: list[str] = field(default_factory=list) # Mutable default pattern访问状态: 在组件或事件处理函数内使用。
state = me.state(State)Event Handlers
事件处理函数
- Regular: Run to completion, then update UI.
- Generator: to stream UI updates (e.g., loading states, LLM streaming). MUST yield at the end.
yield - Async: Use and
async deffor concurrent operations.await
- 常规函数:运行至完成后更新UI。
- 生成器函数:使用来流式更新UI(例如加载状态、LLM流式输出)。必须在末尾使用yield。
yield - 异步函数:使用和
async def处理并发操作。await
Component Composition
组件组合
Use blocks for content components (parents):
withpython
with me.box(style=me.Style(display="flex")):
me.text("Child 1")
me.button("Child 2")使用块来组织内容组件(父组件):
withpython
with me.box(style=me.Style(display="flex")):
me.text("Child 1")
me.button("Child 2")Key Usage
Key的使用
Use to identify components for:
key- Resetting state (change key to re-render)
- Focus management ()
me.focus_component - Scroll targeting ()
me.scroll_into_view - Reusing event handlers (access )
e.key
使用来标识组件,用于:
key- 重置状态(修改key以重新渲染)
- 焦点管理()
me.focus_component - 滚动定位()
me.scroll_into_view - 复用事件处理函数(访问)
e.key
Common Pitfalls
常见陷阱
- Closure Variables: Do NOT rely on closure variables in event handlers; they may be stale. Use or state instead.
key - Input Race Conditions: Avoid setting on inputs unless necessary. Use
valueinstead ofon_blurfor performance, or track "initial" vs "current" value if bidirectional binding is needed.on_input - Mutable Defaults: Never use mutable types (list, dict) as default values in . Use
@me.stateclass.dataclasses.field(default_factory=...) - Global State: Global variables are shared across ALL users. Use for per-session state.
@me.stateclass
- 闭包变量:请勿在事件处理函数中依赖闭包变量,它们可能已过期。请改用或状态。
key - 输入竞态条件:除非必要,否则避免为输入组件设置。为提升性能,使用
value替代on_blur;若需要双向绑定,请跟踪“初始值”与“当前值”。on_input - 可变默认值:切勿在中使用可变类型(列表、字典)作为默认值。请使用
@me.stateclass。dataclasses.field(default_factory=...) - 全局状态:全局变量会在所有用户之间共享。请使用来存储会话级状态。
@me.stateclass
Reference Map
参考指南
- Core Patterns: (State, Events, Streaming, Navigation)
references/core-patterns.md - Components: (API reference for all components)
references/components-reference.md - Styling & Layouts: (CSS-in-Python, Flexbox, Grid, Theming)
references/styling-and-layouts.md - Deployment: (Cloud Run, Docker, Config, Security)
references/deployment-and-config.md - Web Components: (Custom JS integration, Lit)
references/web-components.md
- 核心模式:(状态、事件、流式输出、导航)
references/core-patterns.md - 组件:(所有组件的API参考)
references/components-reference.md - 样式与布局:(Python中的CSS、Flexbox、Grid、主题)
references/styling-and-layouts.md - 部署:(Cloud Run、Docker、配置、安全)
references/deployment-and-config.md - Web组件:(自定义JS集成、Lit)
references/web-components.md