Last active
April 28, 2017 17:22
-
-
Save vabarbosa/fc3abadbcf60be3bcfdf7119ec1de275 to your computer and use it in GitHub Desktop.
PixieDust Extensibility API - Word Cloud Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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