3 Replies Latest reply on Jul 5, 2017 1:29 PM by Prayson Wilfred Daniel

    Unable to execute python code from calculated field using TabPy

    Tim van Elteren

      Hi All,

       

      I have a question regarding the use of the TabPy connector: https://github.com/tableau/TabPy

       

      For installation I followed the steps in the github: https://github.com/tableau/TabPy/blob/master/server.md#setup-on-windows so that it is installed on the same machine as Tableau Desktop (version 10.1.5, Windows 10).

       

      I installed the TabPy server, by downloading the zip and running the startup.bat, this in turn installs Anaconda and after a while it mentions it is running and listening on port 9004.

       

      Next I perform the steps in Tableau desktop to connect to it: Help > Settings and Performance > External Service Connection, and add as server localhost and port 9004. I check the connection and it responds that it can communicate with the Predictive Service.

       

      Using the blog found here: https://www.tableau.com/about/blog/2016/12/using-python-sentiment-analysis-tableau-63606 I performed a test drive. I removed all the comments to have the calculated field accept the code, and get to the following;

       

      SCRIPT_REAL(
      "
      from nltk.sentiment import SentimentIntensityAnalyzer

      text = _arg1
      scores = []
      sid = SentimentIntensityAnalyzer()

      for word in text:
      ss = sid.polarity_scores(word)
      scores.append(ss['compound'])

      return scores"
      ,ATTR([Word]))

       

      When executing the calculated field, it returns;

       

      Error processing script
      ImportError : No module named sentiment

       

      The blogpost found here nltk/nltk#1259 points to a version issue wrt NLTK therefore I upgraded conda, python and nltk accordingly, with all versions meeting requirements.

       

      After restarting the python server it still responds with;

       

      ERROR:main:{"info": "ImportError : No module named sentiment", "ERROR": "Error processing script"}
      ERROR:tornado.access:500 POST /evaluate (::1) 35.00ms

       

      Okay, so maybe something is still an issue related to the NLTK package, so I try steps found in another blog, where the following calculated field is defined;

       

      SCRIPT_REAL(
      "import numpy as np
      return np.corrcoef(_arg1,_arg2)[0,1]",SUM([Sales]),SUM([Profit]))

      this results in no errors but warnings;

       

      C:\Users<user>\Anaconda\envs\Tableau-Python-Server\lib\site-packages\numpy\lib\function_base.py:1890: RuntimeWarning: Degrees of freedom <= 0 for slice
      warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning)
      C:\Users<user>\Anaconda\envs\Tableau-Python-Server\lib\site-packages\numpy\lib\function_base.py:1901: RuntimeWarning: invalid value encountered in true_divide
      return (dot(X, X.T.conj()) / fact).squeeze()

       

      yet nothing happens in the accompanied workbook or views.

       

      In some other case this script results in the output;

       

      Error processing script
      TypeError : unsupported operand type(s) for +: 'float' and 'NoneType'

       

      Any clue why the tableau python server (seems to) refuse to handle the requests from calculated fields with python scripts in Tableau?

       

      I also opened the issue on the TabPy GitHub page: https://github.com/tableau/TabPy/issues/21

       

      Kind regards,

       

      Tim