You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular应用中通过API获取Superset仪表盘缩略图

问题:Superset仪表盘缩略图API返回异步消息,无法直接获取图片

我正在将Superset集成到我的Angular应用中,在获取Superset仪表盘缩略图时遇到了问题。目前我使用的API端点为:
/api/v1/dashboard/{pk}/thumbnail/{digest}/
我传入了必要的参数(pk、digest),并将"force"参数设为false,得到的响应为:{"message":"OK Async"}。

以下是我当前的Angular代码片段:

// Angular service code
this.thumbnailService.getThumbnail(pk, digest).subscribe(
  (response) => {
    // Currently, I'm getting {"message":"OK Async"} in the response.
    // I want to get the actual image instead.
    console.log('Response:', response);
  },
  (error) => {
    console.error('Error fetching thumbnail:', error);
  }
);

我的目标是直接在响应中获取缩略图图片,而非异步消息。我已查阅Superset文档,但未找到明确的解决方案。如果有相关经验的开发者能指导我如何直接获取图片,我将不胜感激!


解决方案

  1. 理解异步响应的含义
    返回{"message":"OK Async"}说明Superset正在后台生成缩略图,此时无法直接返回图片,需要等待生成完成后再发起请求。

  2. 调整请求参数与轮询逻辑

  • 尝试将force参数设为true,强制API返回已生成的缩略图(如果生成完成)。
  • 若仍收到异步响应,实现轮询逻辑:收到异步消息后,间隔1-2秒重复调用API,直到响应返回图片数据。
  1. 修改Angular代码处理图片响应
    API返回的是图片二进制数据,需指定响应类型为blob才能正确解析:
// 修改thumbnailService中的getThumbnail方法
getThumbnail(pk: string, digest: string): Observable<Blob> {
  const params = new HttpParams().set('force', 'true');
  return this.http.get(`/api/v1/dashboard/${pk}/thumbnail/${digest}/`, {
    responseType: 'blob',
    params: params
  });
}

// 组件中封装调用逻辑
private fetchThumbnail(pk: string, digest: string): void {
  this.thumbnailService.getThumbnail(pk, digest).subscribe(
    (blob) => {
      // 将blob转换为可显示的图片URL
      const imageUrl = URL.createObjectURL(blob);
      // 可将imageUrl赋值给img标签的src属性
      console.log('Image URL:', imageUrl);
    },
    (error) => {
      // 处理异步响应,发起轮询
      if (error.error?.message === 'OK Async') {
        setTimeout(() => this.fetchThumbnail(pk, digest), 1500);
      } else {
        console.error('Error fetching thumbnail:', error);
      }
    }
  );
}

// 调用方法
this.fetchThumbnail(pk, digest);
  1. 确认权限与生成状态
    确保请求携带正确的认证信息(如JWT token),拥有访问该仪表盘缩略图的权限。也可在Superset后台手动触发缩略图生成后,再调用API尝试获取。

内容的提问来源于stack exchange,提问作者Mian Awais

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:17:03