Getting Started¶
Installation¶
the easiest way to install is with pip
# install from pipi
pip install bokeh_wordcloud2
# install from github
pip install git+https://github.com/joranbeasley/bokeh_wordcloud2.git#bokeh_wordcloud
# clone and build
git clone pip git+https://github.com/joranbeasley/bokeh_wordcloud2.git
cd bokeh_wordcloud2
pip install . # alternatively `python setup.py install`
Examples¶
Interactive Jupyter Notebook¶
Feel free to play with the jupyter notebook available at https://gke.mybinder.org/v2/gh/joranbeasley/bokeh-wordcloud-notebook/master?filepath=bokeh_wordcloud2.ipynb
First Word Cloud¶
here is a super simple word cloud to get you started
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh_wordcloud2 import WordCloud2
data = [
['susan',6], ['tom',3], ['frankie',8],
['roger',7], ['amy',9], ['nicole',10],
['joran',5], ['mark',4], ['brianne',7],
['michael',8], ['greg',4], ['adrian',6],
['drew',9]
]
names,weights = zip(*data)
test1 = ColumnDataSource({'names':names,'weights':weights})
# we will specify just "blue" for the color
wordcloud = WordCloud2(source=test1,wordCol="names",sizeCol="weights",colors="blue")
show(wordcloud)
|
when you run the application you should see a webpage open with your newly created Wordcloud
data = [ ['susan',6], ['tom',3], ['frankie',8], ['roger',7], ['amy',9], ['nicole',10], ['joran',5], ['mark',4], ['brianne',7], ['michael',8], ['greg',4], ['adrian',6], ['drew',9] ] names,weights = zip(*data) test1 = ColumnDataSource({'names':names,'weights':weights}) sdp = WordCloud2(source=test1,wordCol="names",sizeCol="weights",colors=['pink','blue','green']) show(sdp)
Using a corpus instead¶
if you dont specify a sizeCol
, then it will extract all the words from the wordCol
and use the counts as a size
Color Options¶
for the color options we can pass in a single color that will be assigned to all words
# all the words are pink
WordCloud2(source=test2,wordCol="titles",colors='pink')
or we can pass in a list, and randomly select from it:
# pick a random color
random_colors=['pink','blue']
WordCloud2(source=test2, wordCol="titles", colors=random_colors)
or if our datasource has a column for colur we can pass in the name of that (only works if you specify sizeCol)
colors = [['red','green','blue','purple'][i%4]
for i in range(len(test1.data['words']))]
test1.data['colorsCol'] = colors # assign a new column
WordCloud2(source=test1,wordCol="names",sizeCol="weights",
colors="colorsCol") # use our column name instead
or we can supply a javascript callback that returns a string, but we name it slightly differently
colorFun = CustomJS(code="""
console.log("PICKING A COLOR FOR:",cb_obj)
console.log("Got Word:",cb_data['word'],cb_data['weight'],cb_data['fontSize'])
return "red" # dont forget to RETURN a value
""")
WordCloud2(source=test1,wordCol="names",sizeCol="weights",colorsFun=colorFun)
Clicks And Hovers¶
you can subscribe to either clicks or hovers with a javascript object
Python Click Callback¶
Note
this only applies when running bokeh server bokeh run my_app.py
you can also subscribe to the click handler in python if you are running with bokeh server
wordcloud = WordCloud2(source=test1, wordCol="names", sizeCol="weights", colors=['pink', 'blue', 'green'])
def clicked_word(evt):
print("GOT:",evt)
data=test1.data
if(7 < evt.weight < 20 ):
new_weight = evt.weight + random.choice([-1,1,1,2])
elif evt.weight < 7:
new_weight = evt.weight + random.choice([1,2])
elif evt.weight < 20:
new_weight = evt.weight - random.choice([1,2,3])
weights = list(data['weights'])
weights[data['names'].index(evt.word)] = new_weight
# make sure to reassign back to data a new dict, or the difference might not be noticed
test1.data = {'names':data['names'][:],'weights':weights}
# subscribe to the click event
wordcloud.on_click(clicked_word)
curdoc().add_root(column(wordcloud))
bokeh serve app.py
I cannot show you the html