Skip to main content




Time to read: 4 minutes

En la Parte 1, presenté algunos escenarios posibles para usar python con Power BI fuera de las funciones integradas de python visual y script de python. Esta publicación se centra en la configuración de un archivo de herramienta externo básico que pasa la conexión del modelo de datos de Power BI para su uso en scripts de python. Va a ser un very basic scenario, but it lays the foundation for hopefully better things to come, as described in Part 1.

Define a basic PBITool JSON file

To see a new tool on the Power BI Desktop ribbon, you must define a JSON file and place it in a specific folder on your workstation. the Featured The external tools Tabular Editor, DAX Studio, and ALM Toolkit have installers that handle this step. Since you don't have a dedicated installer to put this file in the required directory, you need to manually define yours.

Siempre que el «formato de metadata mejorado» para el modelo de datos esté habilitado y el JSON en su archivo be preciso, debería ver su nueva herramienta en la cinta después de volver a abrir Power BI Desktop.

Definamos una herramienta externa llamada «Conexión de escritorio de Power BI» que muestra el Logo de python en la cinta y muestra la información de conexión de Power BI actual cuando hace clic en ella.

<>

To get started, copy and paste the following JSON into a text editor (essence).

{
  "version": "1.0",
  "name": "[Tool Name]",
  "description": "[Tool Description]",
  "path": "C: \[PATH TO PYTHON EXECUTABLE]\ python.exe ",
  "arguments": "C: /[PATH TO PYTHON SCRIPT].py "%server%" "%database%"",
  "iconData": "data: image / png; base64,[YOUR BASE64 IMAGE CONTENT]"
}

Here is an example that I completed for my basic sample tool to show the current Power BI Desktop connection. I'm using Anaconda, that's why you see python.exe from the default Anaconda location in my path.

 {"version": "1.0", "name": "Power BI Desktop Connection", "description": "Use python", "path": "C: \ ProgramData \ Anaconda3 \python.exe"," arguments ":" C: / Users / DavidEldersveld / Documents / python /PowerBIConnectionString.py "%server%" "%database%"", "iconData": ""}

Desglosando el contents:
version: don't change this
Name: display name on Power BI Desktop ribbon
description: descripción de la herramienta externa (pensaría que esto aparecería como texto alternativo cuando se desplaza, pero solo el Name displayed when hovering)
way: way to you python.exe proceedings
arguments: command line arguments passed to your executable
iconData: base64 image used for ribbon icon

If you look at the [tool] .pbitool.json for another tool like Tabular Editor, you will see the server and database passed as the only two arguments. For use with python, those can remain, but your first argument will need to be a python .py script in this case.

To adapt this file to yours, determine the appropriate values and replace them as necessary.

 {"version": "1.0", "name": "[Tool Name]"," description ":"[Tool Description]"," path ":" C: \[PATH TO PYTHON EXECUTABLE]\ python.exe "," arguments ":" C: /[PATH TO PYTHON SCRIPT].py "%server%" "%database%"",
  "iconData": "data:image/png;base64,[YOUR BASE64 IMAGE CONTENT]"
}

The two most important will be the correct path to your python.exe and the first argument value with the correct path to your python script.

Sé que hay una forma de usar código para convertir imágenes a base64, pero siempre termino usando un sitio Web como este por conveniencia: https://www.base64-image.de/

Add your [tool] .pbitool.json file to the correct folder

Add your new file to your C: Program Files (x86) Common Files Microsoft Shared Power BI Desktop External Tools binder.

Create your python script

The [tool] .pbitool.json file used a .py file as the first argument. Where did that come from? It comes from you! Put the code you want to run in that file and save it in the location you provided in the [tool] .pbitool.json file.

For my Power BI Desktop Connection example, my sample script (essence) is below, which I saved as PowerBIConnectionString.py in the path described above [tool] .pbitool.json file. The script simply prints the server and database arguments passed to it, then concatenates and prints a connection string that could be used to connect to that particular data model.

import sys print ('Power BI Desktop Connection') print (str (sys.argv [1])) print (str (sys.argv [2])) print ('') conn = "Provider = MSOLAP; Data Source = "+ str (sys.argv [1]) +"; Initial Catalog = ''; " print (conn) input () 

sys.argv [1] is the argument corresponding to the server and sys.argv [2] is the GUID of the database. python starts at 0, which is the value of the argument associated with the current script.

If you finally extend the external tools with your own python scripts and pass the Power BI Desktop connection, you will trust sys.argv [1].

The GUID of the database will come into play in future scripts, but it is not required for this initial scenario. Just know that locally the connection string will only need server: port.

If both the .py script and [tool] The .pbitool.json file are in the correct location, and assuming the script works, you can open Power BI Desktop. Check the External Tools ribbon to run the script.

Coming soon in Part 3: .NET + python - Connecting to the Tabular Object Model (TOM)

While this basic script works as is, there is an issue with this setup that I will cover in the next post. python.exe runs independently outside of an environment, so packages loaded into the database or in an alternate virtual environment are not available to the script.

To connect to python's tabular object model, we'll need a little help from .NET and some packages in my conda environment. Part 3 contains the settings to the [tool] .pbitool.json file that allows me to use a specific python virtual environment.

Payment links

Como asociado de Amazon, gano con las compras que califican.