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

Python script error within Tableau connecting to Mapbox - KeyError #403

Closed rafmirza closed 4 years ago

rafmirza commented 4 years ago

I am currently trying to execute the following script within Tableau, to which I receive the following error.

Background: Trying to show retrieve isochrones from the Mapbox service into Tableau using TabPy integration.

SCRIPT_REAL(" import requests import json time = _arg4 transit= _arg5 longitude=_arg1 latitude=_arg2 url=''.join(['https://api.mapbox.com/isochrone/v1/mapbox/',str(transit[0]),'/',str(longitude[0]),',', str(latitude[0])]) payload={'contours_minutes': str(time[0]),'polygons': 'true','access_token':''} r=requests.get(url, params=payload) rjson=r.json() result_len = len(_arg3) j=rjson['features'][0]['geometry']['coordinates'] print(len(j[0])) j2=[] if len(j) < result_len: j2 = j[0] + ([[None,None]] * (result_len-len(j[0]))) point = [i for x in j2 for i in x] return(point[0::2]) ",MAX([LongitudeDensify]),MAX([LatitudeDensify]),MAX([Densify2]),[Distance],[Transit Method])

image

Any ideas on how to fix this issue?

0golovatyi commented 4 years ago

@rafmirza Looks like Python is complainin about j=rjson['features'][0]['geometry']['coordinates'] and saying there's no features property in rjson. Does the script work by itself (outside of Tableau)?

rafmirza commented 4 years ago

@0golovatyi Quite new to this, and not 100% sure how to run this script outside of Tableau. I had it in VSCode, through which it was returning "undefined variable" for "_arg(n)'s". As issue which was not being raised within the Tableau script.

I did a pip install rjson thinking perhaps I might not have the package. But still the same KeyError is coming up. Thoughts?

0golovatyi commented 4 years ago

Tableau is just replacing _argX with the parameters you provided in SCRIPT_.... So you should be able to call the code with Python passing some values for all the _arg's.

E.g. wrap your code in a function that takes _arg1, _arg2 and so on arguments and call it with some data. More details here - https://github.com/tableau/TabPy/blob/master/docs/TableauConfiguration.md#anatomy-of-a-python-calculation