8

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)

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Benjamin Audet
  • 463
  • 3
  • 12
  • Have you tried running power bi from cmd and watching that cmd? Can't try it now but just head over to the .exe and just run it pbi.exe and check run your python script while checking the output in the terminal window – Gustavo Morais May 21 '19 at 15:14

4 Answers4

14

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

Benjamin Audet
  • 463
  • 3
  • 12
  • But that will stop the script? – Chadee Fouad Nov 13 '21 at 23:10
  • @ChadeeFouad yeah. But it doesn't matters since it's for "light" debugging purposes. I have read your suggestions, that's smart to use tkinter or file I/O. Still, I prefer to have an exception thrown/raised so I can follow the traceback of the script for example. – Benjamin Audet Jul 12 '22 at 14:22
2

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().

vestland
  • 55,229
  • 37
  • 187
  • 305
  • Hey, thanks for your answer. Effectively I can fill dataset in order to print, but I've find a faster way to do it and above all, more in a console way : I `raise Exception(WhatIWantToPrint)` and it display whatever I want through an exception, not the best, but perfect to check variables using the Pythin interpretor debugger – Benjamin Audet Jun 05 '19 at 13:10
  • @Benjamin Audet Cool. Would you care to share your approach? – vestland Jun 05 '19 at 13:12
  • for example, I want to print all variables contained in the globals() of python I do a `raise Exception(globals())` and it display the yellow block containing the exception – Benjamin Audet Jun 05 '19 at 13:18
  • And about the access_token, I finally get it like this : `dataset.iloc[0, 0]` If you are interested by the code : https://github.com/bimdata/BIMDataToPowerBI – Benjamin Audet Jun 05 '19 at 13:19
  • @BenjaminAudet That's interesting. Please consider writing it up as an answer to your question. I'm sure it will attract an upvote or two. – vestland Jun 05 '19 at 13:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194487/discussion-between-benjamin-audet-and-vestland). – Benjamin Audet Jun 05 '19 at 13:31
0

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

Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29
0

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")
Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29