Power BI服务报表连接报错:@jupyter-widgets/base模块未注册
Power BI报表嵌入Jupyter时的Widget依赖错误
问题场景
已完成设备流认证(代码中使用client_credentials方式成功获取Access Token),但在Jupyter环境中创建Report对象时触发如下Widget相关错误:
[Open Browser Console for more detailed log - Double click to close this message]
Failed to load view class 'ErrorWidgetView' from module '@jupyter-widgets/base'
Error: No version of module @jupyter-widgets/base is registered
实现代码
from powerbiclient import Report, models import requests tenant_id = 'AAA' client_id = 'BBB' client_secret = 'CCC' # 注意保密! url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token' headers = { 'Content-Type': 'application/x-www-form-urlencoded' } body = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret, 'scope': 'https://analysis.windows.net/powerbi/api/.default' } response = requests.post(url, headers=headers, data=body) if response.status_code == 200: # 从响应中解析Access Token access_token = response.json().get('access_token') # print("Access Token acquired successfully!") # print(access_token) # 仅用于调试,生产环境不要打印密钥 else: # 如果Token请求失败,打印错误详情 print(f"Error: {response.status_code}") print(f"Error response: {response.json()}") group_id="DDD" report_id="EEE" report = Report(group_id=group_id, report_id=report_id, auth=access_token) report
环境版本
- powerbiclient : 3.1.1
- ipywidgets : 8.1.3
- jupyter-ui-poll: 1.0.0
- msal : 1.30.0
- pandas: 2.2.2
- pyspark : 3.5.2
- requests : 2.32.2
解决方案
1. 修复Jupyter Widgets扩展
执行以下命令重新安装并启用widget扩展:
# 卸载现有ipywidgets pip uninstall -y ipywidgets # 重新安装对应版本 pip install ipywidgets==8.1.3 # 启用Notebook扩展 jupyter nbextension enable --py widgetsnbextension # 如果使用JupyterLab,安装Lab扩展 jupyter labextension install @jupyter-widgets/jupyterlab-manager
完成后重启Jupyter服务再运行代码。
2. 版本兼容性调整
若上述步骤无效,尝试降级ipywidgets到7.x版本(powerbiclient对8.x版本可能存在兼容性问题):
pip install ipywidgets==7.7.1
重启Jupyter后测试。
3. 清除缓存
删除Jupyter的缓存目录(如用户主目录下的.jupyter/nbextensions、.jupyter/labextensions),然后重启服务,避免缓存导致的加载异常。
内容的提问来源于stack exchange,提问作者user1702932
相关产品推荐
相关产品推荐

