Skip to content
Back to skills

Flask实时刷新Matplotlib图像 后台调度 内存缓存

ASecurity

使用Flask、Matplotlib、APScheduler和Flask-Caching构建实时数据可视化应用。图像在后台定期更新并存储在内存缓存中,不保存到本地磁盘,通过HTTP接口提供给前端。

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
toolspythonflask

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill 'flask实时刷新matplotlib图像-后台调度-内存缓存' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Flask实时刷新Matplotlib图像 后台调度 内存缓存?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Flask实时刷新Matplotlib图像 后台调度 内存缓存
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-flask-matplotlib/badge)](https://www.skillsdirectory.com/skills/david-li0406-flask-matplotlib)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "1864480a-56a0-4933-8cb3-e7b381eba5ec"
name: "Flask实时刷新Matplotlib图像(后台调度+内存缓存)"
description: "使用Flask、Matplotlib、APScheduler和Flask-Caching构建实时数据可视化应用。图像在后台定期更新并存储在内存缓存中,不保存到本地磁盘,通过HTTP接口提供给前端。"
version: "0.1.0"
tags:
  - "Flask"
  - "Matplotlib"
  - "APScheduler"
  - "Flask-Caching"
  - "实时图表"
triggers:
  - "flask实时刷新plt图像"
  - "flask后台更新matplotlib图表"
  - "flask_caching缓存matplotlib图像"
  - "flask不保存图片到本地直接显示"
---

# 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不保存图片到本地直接显示

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…