Skip to content

Instantly share code, notes, and snippets.

#SCRIPT_REAL is a function in Tableau which returns a result from an external service script. It's in this function we pass the python code.
SCRIPT_REAL("from nltk.sentiment import SentimentIntensityAnalyzer
text = _arg1 #you have to use _arg1 to reference the data column you're analyzing, in this case [Word]. It gets word further down after the ,
scores = [] #this is a python list where the scores will get stored
sid = SentimentIntensityAnalyzer() #this is a class from the nltk (Natural Language Toolkit) library. We'll pass our words through this to return the score
for word in text: # this loops through each row in the column you pass via _arg1; in this case [Word]
ss = sid.polarity_scores(word) #passes the word through the sentiment analyzer to get the score
#You can manually create a Python 2.7 environment.
conda create --name Tableau-Python-Server python=2.7 anaconda
#then activate it
#and do the pip install from local folders
pip install -r ./tabpy-server/requirements.txt
pip install ./tabpy-client
INFO:__main__:{"INFO": "Loading state from state file"}
INFO:__main__:{"INFO": "Initializing tabpy"}
INFO:__main__:{"INFO": "Done initializing tabpy"}
INFO:__main__:{"INFO": "Web service listening on port 9004"}
# this code is run in your terminal
# navigate to where you saved TabPy, for me that's in my desktop. Update the path location appropriately.
cd ~/Desktop/TabPy
# install TabPy
./setup.sh
import pandas as pd
import time
from nltk.sentiment import SentimentIntensityAnalyzer
t0 = time.time()
top_100 = pd.read_csv('/Users/brit.cava/Desktop/TabPy/top100.csv')
text = top_100['Word']
sid = SentimentIntensityAnalyzer()