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 get and show only one value from a list #354

Closed qvbobo closed 4 years ago

qvbobo commented 5 years ago

For example, I want to define my own function to calculate the average of a list, then just show the float number like the AVG function in tableau. Actually the dimension of result is lower, I don't need the line chart or any chart.

` def myavg(inputx): import numpy as np return np.array(inputx).mean()

`

SCRIPT_REAL(" return tabpy.query('myavg',_arg1)['response']", ATTR([x]))

The problem is there is no data if I didn't set the columns and rows. How can I do this ? Thank you.

0golovatyi commented 5 years ago

@qvbobo What do you mean by "there's no data"? No data in x field or no data returned? Is there some error message showed by Tableau or in TabPy logs?

qvbobo commented 5 years ago

SCRIPT_REAL(" import numpy as np return np.array(_arg1).mean() ", ATTR([x]))

When x=[1,2,3,4,5], if I set the columns to be x, and the rows "myavg", then I got results of [3,3,3,3,3] and a horizontal line. How can I get only one number "3"?

qvbobo commented 5 years ago

I want to show the result number like https://community.tableau.com/message/319034#319034. But "myavg" cannot be calculated if the columns and rows are empty.

0golovatyi commented 5 years ago

As https://github.com/tableau/TabPy/blob/master/docs/TableauConfiguration.md#anatomy-of-a-python-calculation explains it "Tableau expects the SCRIPT to return a single column that has either a single row or the same number of rows as it passed to TabPy." In case user script returns exactly one value Tableau itself replicates it for each row of data behind the viz.