Benjamin Audet
Benjamin Audet

Reputation: 493

Python console in Power BI

I'm currently working of Power BI with python scripting. I'd like to print (using the function print) but I'm not able to find a way to see my printed message anywhere.

I've already search on Google and Stack Overflow if a console exists on Power BI to have the Python output

import pandas as pd

dataset = pd.DataFrame(dataset.loc[:1, 'access_token'])

access_token = dataset.iloc[0]
print(access_token)

I'd like to have the output of print(access_token)

Upvotes: 9

Views: 7218

Answers (4)

Chadee Fouad
Chadee Fouad

Reputation: 2948

Another way of doing this is making python in PBI open notepad and write whatever you need there from the df (while it's open) so that you could see what you need. Here's the code:

import os
f = open('foo.txt','w')
f.write('Hello world!')
f.close()
os.system("notepad.exe foo.txt")

Upvotes: 1

Chadee Fouad
Chadee Fouad

Reputation: 2948

It's not the easiest thing but should work. Use Tkinter to display a form with your message...something like this:

import tkinter as tk
master = tk.Tk()
HelloWorld = "HelloWorld   "
msg = tk.Message(master, text = HelloWorld )
msg.config(bg='lightgreen', font=('times', 24, 'italic'))
msg.pack()
tk.mainloop()

enter image description here

Upvotes: 0

Benjamin Audet
Benjamin Audet

Reputation: 493

I've finally found an efficient way to print debug in Python in Power BI.

As long as we cannot use the function print because we don't have a display of the standard output of Python in Power BI, we can raise exception in order to display a variable or anything else.

You can use raise Exception(TheVariableYouWantToPrint)`

For example, you can do raise Exception(dataset) if you want to check the content of the dataset global variable

You can also print a string like raise Exception("Hello World")

It's not the best way to print something but still, that's the only way I've found to do it easily and efficiently

Upvotes: 16

vestland
vestland

Reputation: 61254

Sorry, no console. But there are other ways to inspect your variables. Not using print() though. You haven't specified whether you're working with a Python visual directly on the Report tab, or the Run Python Script option under the Transform tab in the Power Query Editor. I'd suggest the latter.

Try this:

1: In the report tab, click Click Home > Enter Data > Load without inserting any data.

2: Click Edit Queries and select Transform > Run Python Script

3: Insert the following snippet in the dialog box:

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

I've just used some sample data since I don't have your dataset. The important thing here is that both df in my sample and dataset in your sample are of type pandas.core.frame.DataFrame.

4: Click OK and you should get this:

enter image description here

Most variables (we'll get to that later) that you import or create should be made available there. In our case we have d and df. Click on table to the right of df to see this:

enter image description here

I hope this helps!


End note:

About the 'Most variables...' part. It seems that whether or not your variables are made available will depend on the datatype of said variable. I tried using a version of the snippet in your question and found that access_token = df.iloc[0] returns a variable of type pandas.core.series.Series. This will for some reason not be available for direct inspection as is the case for d and df that are of type dict and pandas.core.frame.DataFrame, respectively. So to make access_token availale to you, you should try access_token = dataset.iloc[0].to_frame().

Upvotes: 2

Related Questions