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

Azure Databricks中powerbiclient嵌入Power BI报告失败问题咨询

问题解决:Azure Databricks中powerbiclient嵌入Power BI报告失败

问题概述

在Azure Databricks(Personal Compute集群,运行时版本15.3)使用powerbiclient==3.1.1包获取Power BI报告信息时,触发Exception: Power BI report is not embedded错误,相同代码在本地Visual Studio Code可正常运行。已尝试设备代码登录和服务主体两种认证方式,需采用服务主体方式;实例化Report后report._embedded属性为False,无法显示报告,调用get_pages()时触发上述错误。

核心原因

powerbiclient的Report类依赖浏览器环境的iframe嵌入能力,而Azure Databricks笔记本的后端执行环境没有完整的前端渲染上下文,导致_embedded属性无法被正确初始化,进而触发后续操作的错误。本地VSCode能正常运行,是因为它可以调用本地浏览器完成嵌入渲染流程。

可行解决方案

方案1:改用Power BI REST API获取报告元数据(无需可视化)

如果仅需获取报告页面、数据集等元数据,不需要可视化嵌入,直接调用Power BI REST API替代powerbiclient的相关方法即可:

  • 沿用现有服务主体认证逻辑获取Azure AD令牌
  • 调用对应REST接口获取所需信息
  • 示例代码:
import requests

# 替换为你的实际值
access_token = "你的服务主体令牌"
group_id = "Power BI工作区ID"
report_id = "目标报告ID"

headers = {"Authorization": f"Bearer {access_token}"}

# 获取报告页面列表
response = requests.get(
    f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/pages",
    headers=headers
)

if response.status_code == 200:
    print("报告页面信息:", response.json())
else:
    print("请求失败:", response.text)

方案2:通过Databricks HTML显示能力实现可视化嵌入

如果需要在Databricks笔记本中可视化显示Power BI报告,可生成Power BI嵌入URL,用Databricks的displayHTML()方法渲染:

  • 用服务主体申请带嵌入权限的嵌入令牌(scope需包含对应报告或工作区的读取权限)
  • 构造嵌入HTML代码(需确保Power BI JavaScript嵌入库已在环境中加载,可通过Databricks集群初始化脚本提前配置)
  • 示例代码:
from databricks import display

# 替换为你的实际值
embed_token = "你的嵌入令牌"
report_id = "目标报告ID"
group_id = "Power BI工作区ID"

html_content = f"""
<div id="reportContainer" style="width: 100%; height: 600px;"></div>
<script>
const embedConfig = {{
    type: 'report',
    id: '{report_id}',
    embedUrl: `https://app.powerbi.com/reportEmbed?reportId={report_id}&groupId={group_id}`,
    tokenType: 1,
    accessToken: '{embed_token}'
}};

const container = document.getElementById('reportContainer');
const report = powerbi.embed(container, embedConfig);

report.on('loaded', () => console.log('报告加载完成'));
</script>
"""

displayHTML(html_content)

关键注意事项

  • powerbiclient的可视化嵌入功能依赖前端浏览器环境,Databricks后端环境不支持,因此无法直接用该包的Report类完成嵌入显示,或调用依赖嵌入状态的方法(如get_pages())
  • 使用服务主体时,需确保已在Power BI管理员门户中启用服务主体访问,并为其分配对应工作区的权限(如成员、管理员)

内容的提问来源于stack exchange,提问作者Ric S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 06:42:13