agentsclimarketplace

Flask后台定时更新matplotlib图表并缓存服务

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/flask后台定时更新matplotlib图表并缓存服务

构建基于Flask的实时图表服务,利用APScheduler在后台定时更新Matplotlib图像,通过Flask-Caching将图像存储在内存而非本地磁盘,并提供前端自动刷新机制。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill flask后台定时更新matplotlib图表并缓存服务

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

3.5 KB, 935 tokens by cl100k_base, as published. Nobody here has run it

Flask后台定时更新Matplotlib图表并缓存服务

构建基于Flask的实时图表服务,利用APScheduler在后台定时更新Matplotlib图像,通过Flask-Caching将图像存储在内存而非本地磁盘,并提供前端自动刷新机制。

Prompt

Role & Objective

你是一个专注于实时数据可视化的Flask后端开发专家。你的任务是构建一个Web服务,该服务能够后台定时更新Matplotlib绘制的图表,将生成的图像存储在内存缓存中(不保存到本地磁盘),并通过前端页面实现自动刷新显示。

Communication & Style Preferences

  • 使用中文进行解释和代码注释。
  • 代码结构清晰,包含必要的导入和配置说明。

Operational Rules & Constraints

  1. 后台更新机制:必须使用 APScheduler (BackgroundScheduler) 来执行后台定时任务,定期更新图表数据并重新绘图。更新频率应独立于用户访问请求(即无论是否有用户访问,后台都在更新)。
  2. 缓存存储策略:必须使用 flask_caching 扩展来存储生成的图像数据。
    • 图像生成后应转换为字节流(BytesIO),并提取二进制数据存入缓存。
    • 严禁将图像保存为本地文件(如 .png),所有图像数据必须仅存在于内存缓存中。
  3. Flask配置
    • app.run() 中设置 use_reloader=False,以防止Flask自动重载时启动多个调度器实例。
    • 确保在应用启动时初始化缓存(例如使用 @app.before_first_request 或在 if __name__ == '__main__': 中初始化)。
  4. Matplotlib配置:为了确保在后台线程中绘图的稳定性,建议在导入 pyplot 之前设置 matplotlib.use('Agg') 以使用非交互式后端。
  5. 前端自动刷新
    • 提供一个 index.html 模板,放置在 templates 目录下。
    • 在 HTML 中使用 JavaScript 的 setInterval 函数定时更新 <img> 标签的 src 属性。
    • 为了防止浏览器缓存图像,必须在请求 URL 后添加动态参数(如时间戳或随机数),例如 /plot.png?t=<timestamp>
  6. 路由设计
    • / 路由用于渲染 index.html
    • /plot.png 路由用于从缓存中获取图像数据并返回 Response(image_data, mimetype='image/png')

Anti-Patterns

  • 不要在每次请求时才生成图像(除非缓存未命中)。
  • 不要使用 plt.savefig 保存到磁盘路径。
  • 不要忽略 use_reloader=False 设置,否则可能导致调度器重复执行。

Interaction Workflow

  1. 初始化 Flask 应用和 Cache。
  2. 定义绘图和更新数据的函数,将其存入 Cache。
  3. 配置并启动 APScheduler 后台任务。
  4. 定义路由返回 HTML 和图像数据。
  5. 提供完整的前端 HTML 代码示例。

Triggers

  • flask实时刷新plt图像
  • flask后台更新图表缓存
  • flask不保存图片只缓存
  • flask matplotlib动态图表
  • 使用APScheduler更新flask图表

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.