在 Jupyter Notebook 中內嵌 Power BI 元件

Jupyter Notebook 讓建立和共用 Power BI 報表變得簡單。 使用 Jupyter Notebook,您可以快速內嵌或建立報表。

powerbiclientPython 套件可讓您輕鬆地在 Jupyter Notebook 中內嵌 Power BI 報表。 您可以將資料從 Power BI 報表中的視覺效果匯出至 Jupyter Notebook,以進行深入的資料探索。 您也可以篩選報表以進行快速分析,或使用書簽來套用已儲存的檢視。

安裝 Power BI 用戶端套件

您可以在 powerbiclientPyPI上找到套件。 它也是 GitHub上的開放原始碼。

若要安裝套件,您可以使用 pip

  • 如果您使用 Jupyter Notebook:

    pip install powerbiclient
    
  • 如果您使用 JupyterLab:

    pip install powerbiclient
    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    
  • 如果您使用 Jupyter Notebook 5.2 或更早版本,您可能也需要啟用 nbextension:

    jupyter nbextension enable --py [--sys-prefix|--user|--system] powerbiclient
    

在 Jupyter Notebook 中內嵌 Power BI 報表

動畫 gif,顯示內嵌在 Jupyter Notebook 中的 Power BI 報表。

如需詳細的使用資訊,請參閱 GitHub Wiki

內嵌報告

此範例示範如何使用套件內嵌 Power BI 報表。

  1. 從封裝匯入報表類別和模型:

    from powerbiclient import Report, models
    
  2. 使用 Azure AD 對 Power BI 進行驗證:

    # Import the DeviceCodeLoginAuthentication class to authenticate against Power BI
    from powerbiclient.authentication import DeviceCodeLoginAuthentication
    
    # Initiate device authentication
    device_auth = DeviceCodeLoginAuthentication()
    
  3. 設定您想要內嵌的工作區識別碼和報表識別碼:

    group_id="Workspace ID"
    report_id="Report ID"
    
  4. 建立 Power BI 報表的實例,並將報表載入輸出資料格:

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

執行示範

GitHub存放庫包含示範 Jupyter Notebook,可內嵌使用者的報表。 它示範內嵌與 Power BI 報表互動的完整流程,包括:

  • 報表事件處理常式
  • 取得頁面清單
  • 取得視覺效果清單
  • 匯出視覺效果資料並將其視覺化
  • 套用篩選條件

若要執行示範:

  1. 確定您已滿足下列先決條件:

    • pandas
    • matplotlib
  2. 執行下列命令:

    cd demo
    jupyter notebook
    
  3. 執行 demo.ipynb

快速建立報表

您也可以在幾個步驟中輕鬆地從您的資料產生 Power BI 快速報表 。 使用筆記本中的任何 DataFrame,將它快速轉換成深入的視覺效果。」 如有需要,請儲存報表,並如同任何其他報表一樣加以使用。

如需詳細描述,請參閱 使用 Jupyter Notebook 快速建立報表。 如需示範 Jupyter Notebook,請參閱 GitHub 存放庫

下一步