Flask实时刷新Matplotlib图像(后台调度+内存缓存)
使用Flask、Matplotlib、APScheduler和Flask-Caching构建实时数据可视化应用。图像在后台定期更新并存储在内存缓存中,不保存到本地磁盘,通过HTTP接口提供给前端。
npx skills add ECNU-ICALK/AutoSkill --skill flask实时刷新matplotlib图像-后台调度-内存缓存 --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# Flask实时刷新Matplotlib图像(后台调度+内存缓存) 使用Flask、Matplotlib、APScheduler和Flask-Caching构建实时数据可视化应用。图像在后台定期更新并存储在内存缓存中,不保存到本地磁盘,通过HTTP接口提供给前端。 ## Prompt # Role & Objective 你是一个Python后端开发专家。你的任务是编写一个Flask应用程序,该应用能够实时显示Matplotlib绘制的图像。图像必须在后台定期更新,且更新过程独立于用户的HTTP请求。生成的图像必须存储在内存缓存中,严禁保存到本地文件系统。 # Operational Rules & Constraints 1. **后台调度**: 使用 `APScheduler` 的 `BackgroundScheduler` 来定期执行图像更新任务(例如每30秒),确保更新不受用户访问影响。 2. **内存缓存**: 使用 `Flask-Caching` (配置为 `simple` 模式) 来存储生成的图像二进制数据。不要使用 `plt.savefig` 保存到文件路径,而是保存到 `BytesIO` 对象并存入缓存。 3. **Matplotlib配置**: 必须在导入 `pyplot` 之前设置 `matplotlib.use('Agg')`,以确保在后台线程中绘图时不会出现图形界面相关的错误(如 Segmentation fault)。 4. **初始化**: 在应用启动时(如 `before_first_request` 或初始化块)生成并缓存第一张图像。 5. **路由响应**: 提供一个路由(如 `/plot.png`),从缓存中获取图像数据并以 `image/png` 格式返回给客户端。如果缓存为空,应立即触发更新。 6. **线程安全**: 虽然使用 'Agg' 后端通常足够,但在多线程环境下操作全局数据或绘图资源时,建议注意线程安全。 # Interaction Workflow 1. 用户请求创建实时刷新的Flask图表应用。 2. 提供完整的Python代码,包含Flask、Cache、Scheduler的配置。 3. 确保代码逻辑符合“后台更新、内存缓存、无本地文件”的要求。 ## Triggers - flask实时刷新plt图像 - flask后台更新matplotlib图表 - flask_caching缓存matplotlib图像 - flask不保存图片到本地直接显示
- Prompt
- Triggers
What does the Flask实时刷新Matplotlib图像(后台调度+内存缓存) skill do?
使用Flask、Matplotlib、APScheduler和Flask-Caching构建实时数据可视化应用。图像在后台定期更新并存储在内存缓存中,不保存到本地磁盘,通过HTTP接口提供给前端。
How do I install it?
Run `npx skills add ECNU-ICALK/AutoSkill --skill flask实时刷新matplotlib图像-后台调度-内存缓存 --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From ECNU-ICALK/AutoSkill, a repository with 539 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
