在 Jupyter 笔记本中嵌入 Power BI 组件

  • 版本 :2023.1(当前版本)

在 Jupyter 笔记本中嵌入 Power BI 组件

Python 包让您可以轻松地将powerbiclientPower BI 报告嵌入到 Jupyter 笔记本中。您可以将数据从 Power BI 报表中的视觉对象导出到 Jupyter notebook 以进行深入的数据探索。您还可以过滤报告以进行快速分析或使用书签来应用已保存的视图。

显示 Jupyter 笔记本中嵌入的 Power BI 报告的动画 gif。

如何在 Jupyter 笔记本中嵌入 Power BI 报告

本节介绍如何将powerbiclient包安装到笔记本中,以及如何使用它来嵌入 Power BI 报表。

有关详细的使用信息,请参阅GitHub wiki。

安装 Power BI 客户端包

powerbiclient包可以在PyPI上找到。它也在GitHub上开源。

要安装您可以使用的包pip

  • 如果您使用的是 Jupyter 笔记本:

    Python复制

    pip install powerbiclient
  • 如果您使用的是 JupyterLab:

    Python复制

    pip install powerbiclient
    jupyter labextension install @jupyter-widgets/jupyterlab-manager

嵌入报告

此示例演示如何使用包嵌入 Power BI 报表。

  1. 从包中导入报表类和模型:

    Python复制

    from powerbiclient import Report, models
  2. 使用 Azure AD 针对 Power BI 进行身份验证:

    Python复制

    # Import the DeviceCodeLoginAuthentication class to authenticate against Power BIfrom powerbiclient.authentication import DeviceCodeLoginAuthentication# Initiate device authenticationdevice_auth = DeviceCodeLoginAuthentication()
  3. 设置要嵌入的工作区 ID 和报告 ID:

    Python复制

    group_id="Workspace ID"report_id="Report ID"
  4. 创建 Power BI 报表实例并将报表加载到输出单元格:

    Python复制

    report = Report(group_id=group_id, report_id=report_id, auth=device_auth)

    report

运行演示

GitHub存储库包含一个嵌入用户报告的演示 Jupyter 笔记本。它演示了嵌入 Power BI 报表并与之交互的完整流程,包括:

  • 报告事件处理程序

  • 获取页面列表

  • 获取视觉列表

  • 导出和可视化视觉数据

  • 应用过滤器