Skip to content

Instantly share code, notes, and snippets.

@vabarbosa
Last active April 28, 2017 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vabarbosa/fc3abadbcf60be3bcfdf7119ec1de275 to your computer and use it in GitHub Desktop.
Save vabarbosa/fc3abadbcf60be3bcfdf7119ec1de275 to your computer and use it in GitHub Desktop.
PixieDust Extensibility API - Word Cloud Template
from pixiedust.display.display import *
from wordcloud import WordCloud
import cStringIO
import base64
class SimpleWordCloudDisplay(Display):
def doRender(self, handlerId):
# convert from dataframe to dict
dfdict = {}
df = self.entity.toPandas()
for x in range(len(df)):
currentid = df.iloc[x,0] or 'NoStreet'
currentvalue = df.iloc[x,1]
dfdict.setdefault(currentid, 0)
dfdict[currentid] = dfdict[currentid] + currentvalue
# create word cloud from dict
wc = WordCloud(background_color="white").fit_words(dfdict)
# encode word cloud image to base64 string
b = cStringIO.StringIO()
wc.to_image().save(b, format="PNG")
img_str = base64.b64encode(b.getvalue())
self._addHTMLTemplateString(
"""
<center><img src="data:image/png;base64,{0}"></center>
""".format(img_str.decode("ascii"))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment