tableau / TabPy

Execute Python code on the fly and display results in Tableau visualizations:
https://tableau.github.io/TabPy/
MIT License
1.56k stars 598 forks source link

How to translate python script into TabPy #272

Closed knozaw closed 5 years ago

knozaw commented 5 years ago

Hello,

I've received an Invalid Syntax Error in tableau calculation: image

When I looked at the tableau calculation, the line 8 is: print([p for p in nx.shortest_path(G,source=[Source Parameter],target=[Target Parameter])]) image

Here is the original script written in JupyterLab: image

In tableau calculation, I've updated the source and target value to be dynamically changed using [Source Parameter] and [Target Parameter].

The [Source Parameter] uses the field list "From". image

The [Target Parameter] uses the field list "To". image

Does anyone know how to translate the python script for tableau calculation?

Sincerely, knozawa

0golovatyi commented 5 years ago

@knozaw As specified in the documentation at https://github.com/tableau/TabPy/blob/master/docs/TableauConfiguration.md#anatomy-of-a-python-calculation you need to use positional arguments in your Python script as _arg1, _arg2, etc. When the script is passed to TabPy there is no notion of fields, columns or anything like that.

nmannheimer commented 5 years ago

@knozaw this video should also help answer some of your questions: https://youtu.be/nRtOMTnBz_Y

knozaw commented 5 years ago

Thank you, @0golovatyi and @nmannheimer.

I looked at those documents and YouTube video. It helped me writing a script in tableau. image

However, exactly the same code in Jupyter gives me "True" output, but TabPy gives me an error message. image

image

Did anyone had the similar issue?

Sincerely, knozawa

0golovatyi commented 5 years ago

My guess is pd.read_csv created different frame then pd.DataFrame.

nmannheimer commented 5 years ago

@knozaw your script needs to have a return value, so try adding: return nx.has_path(....

knozaw commented 5 years ago

Thank you, @0golovatyi and @nmannheimer.

I've added return in front of the nx.has_path image

However, I received the same issue: image

nmannheimer commented 5 years ago

@knozaw it looks like your parameter you are passing is a string, but in your jupyter code, you are looking for integers. What happens if you convert the parameter type in Tableau to integer, or convert the strings in your python code to ints?

WillAyd commented 5 years ago

Instead of using os.chdir does it work if you specify the full path to the file in pd.read_csv? The error is getting raised by networkx and if it works on a Jupiter notebook but not on Tableau server I would guess that os.chdir isn't doing what you expect it to on TabPy

0golovatyi commented 5 years ago

@knozaw Has the issue been resolved or you are still experiencing it?

knozaw commented 5 years ago

Hello,

Sorry. I hadn't had a chance to test converting the parameter type and pd.read_csv yet. I'll get back to this once as soon as possible.

Thank you all for your kind help!

Sincerely, knozawa

knozaw commented 5 years ago

@nmannheimer - I tested converting str to int in tableau, but it didn't work either.

@WillAyd - pd.read_csv worked on jupyter.

When I tried to filter by the calculation in tableau, I received this error message: image

It seems like pandas.DataFrame is not working correctly in tableau.

Sincerely, knozawa

WillAyd commented 5 years ago

@WillAyd - pd.read_csv worked on jupyter.

Did you specify a full path to the file in read_csv in both Jupyter and TabPy? Using os.chdir may seemingly be fragile on the TabPy side since you are relying on the server to actually execute that and update the current directory, which it might not guarantee execution of. If you use an absolute path in TabPy you would remove that confounding factor

knozaw commented 5 years ago

Hi @WillAyd,

No. At first, I specified the full path using the os.chdir in the Jupyter Lab. But next, I uploaded the csv file into the Jupyter Lab and used the pd.read_csv("ToFrom.txt", delimiter = '\t') to read the file on the Jupyter Lab. Both ways worked on Jupyter Lab.

I didn't use the full path on TabPy. I read the data from Tableau data source using df = pd.DataFrame({'source':_arg1,'target':_arg2}). image

According to the command prompt, I'm getting 500 post error (internal server error) message. image

Sincerely, knozawa

WillAyd commented 5 years ago

Are you sure your dtypes are consistent across both sources? What is the output of df.dtypes in Jupyter?

knozaw commented 5 years ago

Hi @WillAyd,

Yes, I believe so.

os.char image

pd.read_csv image

Tableau image

The data type is integers for all data sources.

Sincerely, knozawa

WillAyd commented 5 years ago

In Tableau your data type is string. Does changing that to a number resolve?