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

Trouble using plots with TabPy #401

Closed ethantjones97 closed 4 years ago

ethantjones97 commented 4 years ago

I would like to be able to use plots created in Python (i.e. plots generated from matplotlib or seaborn) in my Tableau workbooks. See the below code in my attempt to make a simple sine plot and show it in a Tableau workbook.

`import json import base64 import numpy as np import math import matplotlib.pyplot as plt from tabpy.tabpy_tools.client import Client connection = Client('http://localhost:9004/')

def SinePlot(freq, amp): x = np.array(np.linspace(1,100,100)) y = []
for i in range(len(x)): y_i = math.sin(x[i]) y.append(y_i) i += 1 fig = plt.figure() ax = plt.subplot(111) ax.plot(x, y) plt.title('Sine Wave') plt.legend('sin') fig.savefig('plot.png') data = {} with open('plot.png', mode='rb') as file: img = file.read() data['img'] = base64.encodebytes(img).decode("utf-8")
answer = json.dumps(data) return answer

connection.deploy('SinePlot Func', SinePlot, 'Plots a sine wave', override = True)

print('done')`

sine plot test

0golovatyi commented 4 years ago

@ethantjones97 Are you trying to deploy a function for TabPy or call it later from Tableau? What are the steps or an example you are following?

ethantjones97 commented 4 years ago

Thank you for getting back to me on this - I am deploying a function for the local server, then calling it from within Tableau. See the calculation for the "sine_plot" variable in the attached image. sine plot test

However when I run this function by itself in a Python IDE, I am able to see the desired plot, as shown below.

plot

In the code I originally posted, I was trying to find a way to translate this image to a JSON so it could be transmitted using the API and displayed in Tableau. What am I doing wrong here? How do I get the sine plot to appear in Tableau?

nmannheimer commented 4 years ago

Hi @ethantjones97 what you are trying to do here, return an entire plot file to be rendered in Tableau, is not something that is currently supported. The way Python integration works is for a Python list data type to be returned and included in the visualization. So you could use a dimension with the X axis you are looking for, and use Python to generate and return the Y axis sine wave values you are looking to visualization. If you are interested in returning the matplotlib visuals specifically, you could look at using the Dashboard Extensions API to create custom visuals: https://tableau.github.io/extensions-api/

This is another example for how the current TabPy integration works: https://youtu.be/nRtOMTnBz_Y

0golovatyi commented 4 years ago

@ethantjones97 In addition to what @nmannheimer said above if you haven't yet consider reading https://github.com/tableau/TabPy/blob/master/docs/TableauConfiguration.md#anatomy-of-a-python-calculation for how Tableau makes a call to TabPy and how it processes the result.

ethantjones97 commented 4 years ago

@0golovatyi @nmannheimer thank you guys very much for the speedy responses. Is there any speculation when the ability to pass rendered images through TabPy might be an available feature?

nmannheimer commented 4 years ago

@ethantjones97 this isn't something that's currently on the roadmap, but it's an interesting idea and certainly something that exists in other products. How would you use that capability if you had it?