WordCloud2 Bokeh API Reference¶
-
class
bokeh_wordcloud2.
WordCloud2
(**kw)¶ Bases:
bokeh_wordcloud2.bokeh_wordcloud2._WordCloud2Meta
Provides a Bokeh Interface to WordCloud (https://wordcloud2-js.timdream.org)
As Such it accepts Most of the same arguments.
Note
- any CustomJS callback will have two variables available cb_obj which is the model, and cb_data which is the data for the CustomJS Callback
- depending on context cb_data will have different properties, but usually cb_data.word and cb_data.weight will be available
- many CustomJS callbacks MUST return a value (see the docs for the various attributes)
-
classes
¶ property type:
Either
(String
,Instance
(CustomJS
) )a class name or function to use … only works if using DOM elements, which are currently unsupported… so this does nothing for now
-
click
¶ property type:
Instance
(CustomJS
)js callback to execute on word click
Note
this is NOT the same as
bokeh_wordcloud2.WordCloud2.on_click()
, which runs inside the backend on a bokeh server, where as this attribute expects CustomJS that is run on the client.cb_data provides: cb_data.word, and cb_data.weight
wordcloud.click = CustomJS(code=''' console.log(`You Clicked!!!!: ${cb_data.word} - x${cb_data.weight}`) ''')
-
color
¶ property type:
Either
(String
,List
(String
),Instance
(CustomJS
) )the color or colors to use when generating the wordcloud
data = ColumnDataSource(data={ words = ['apple','pie','tastes','delicious'], weights = [11,10,20,15], colors=['red','blue','blue','green'] }) # a single color, all words will be pink on a blue background wc1 = WordCloud(source=data,wordCol="words", sizeCol="weights", color="pink", background="blue") # 2 colors that will be selected at random, on a yellow background wc2 = WordCloud(source=data,wordCol="words", sizeCol="weights", background="yellow",color=["blue","red"]) # specify a column to use for the colors wc3 = WordCloud(source=data,wordCol="words", sizeCol="weights", background="yellow",color="colors") # specify a javascript callback ,(default white background) callback = CustomJS(code=''' if cb_data.word == 'apple': return 'red' return 'blue' ''') wc4 = WordCloud(source=data,wordCol="words", sizeCol="weights", color=callback)
-
fontFamily
¶ property type:
String
the fontFamily to use.
-
fontWeight
¶ property type:
Either
(String
,Instance
(CustomJS
) )the font weight to use, or a CustomJS that returns a Font weight(eg. ‘bolder’,‘600’,’normal’) (see cb_object)
-
gridSize
¶ property type:
Float
the distance between words, the bigger the gridsize, the more distance between words.
-
hover
¶ property type:
Instance
(CustomJS
)js callback to execute on word hover 1
cb_data provides: cb_data.word, and cb_data.weight
wordcloud.hover = CustomJS(code="console.log(`Hover On: ${cb_data.word} - x${cb_data.weight}`)")
-
maxRotation
¶ property type:
Float
the maximum amount(in radians) to rotate
-
minRotation
¶ property type:
Float
the minimum amount(in radians) to rotate
-
on_click
(python_callback)¶ bind a python callback to word clicks, this only works when running a bokeh server
Note
this is NOT the same as
bokeh_wordcloud2.WordCloud2.click
, which runs on the clients browser, where as this method expects a python function that is run on the bokeh-server.the function that recieves the call back will recieve WordClick Event, that has a word and weight attribute
def my_handler(event): print("Clicked Word: %r"%event.word) # update the view filters some_view.filters = [1,2,4,5,7,8] wordcloud.on_click(my_handler)
Parameters: python_callback – a python function to call when a word is clicked
-
rotateRatio
¶ property type:
Float
the odds of a given word rotating between 0-1, if 1 then the word will ALWAYS rotate, if 0 it will NEVER rotate, at 0.2 it has a 20% chance of rotating
-
rotationSteps
¶ property type:
Int
the number of slices to cut the rotation range into
-
shape
¶ property type:
Enum
( Enumeration(circle, cartoid, diamond, square, triangle-forward, triangle, pentagon, star) )the shape of the wordcloud
-
sizeCol
¶ property type:
String
the column of the weights, if unspecified it will count word occurences
-
source
¶ property type:
Instance
(DataSource
)required The source of data for the widget.
data = ColumnDataSource(data=dict(words=list("ABCDE"),sizes=[1,4,2,5,7])) WordCloud(source=data, wordCol="words", sizeCol="sizes", color="blue")
-
view
¶ property type:
Instance
(CDSView
)A view into the data source to use when rendering table rows. A default view of the entire data source is created if a view is not passed in during initialization.
view = CDSView(source=data,filter=GroupFilter(column_name="active",value="true")) WordCloud(source=data, view=view, wordCol="words", sizeCol="sizes", color=["red","blue"])
-
weightFactor
¶ property type:
Either
(Instance
(CustomJS
),Float
)a multiplier to apply to the sizes or a CustomJS instance(see cb_data)
# you can just specify a number (eg multiply all sizes by 12) wc = WordCloud(source=data,wordCol="words", sizeCol="weights", weightFactor=12) # or you can specify a callback (eg cube the given size) callback = CustomJS(code="return Math.pow(cb_data.size, 3))
-
wordCol
¶ property type:
String
required the column with the words in it