I am trying to use a word cloud in my Dashboard. I have multiple topics from which the cloud can be generated and I have a dropdown that selects the topic. But I seem to do something wrong because the word cloud is not shown in the dashboard.
This is my code:
import dash
from dash import html
import dash_core_components as dcc
import plotly.express as px
import numpy as np
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from dash import Dash, dash_table
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import base64
from io import BytesIO
app = dash.Dash(__name__)
app.layout = html.Div( children=[
html.Div([
dcc.Dropdown(
id='topicwc_drop',
options=[
{'label': f'Topic {i}', 'value': i} for i in topics.keys()],
value=0),
html.Img(id="image_wc")])
])
@app.callback(
Output('image_wc', 'src'),
Input(topicwc_drop, 'value'))
def plot_wordcloud(topicwc_v):
text = {word: value for word, value in model_bert.get_topic(topicwc_v)}
wc = WordCloud(background_color="white", max_words=1000)
wc.generate_from_frequencies(text)
wc.to_image()
img = BytesIO()
wc.save(img, format='PNG')
return 'data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode())
if __name__ == "__main__":
app.run_server(host="localhost",port=8003)
‘topics’ is a dictionary and the topic range is -1 to 94. The model_bert generates the topics.
def update_dropdown_options(year_v, journal_v, topic_v, row_v, topicwc_v):
dff = docs_info.copy()
if year_v:
dff = dff[dff.Year.isin(year_v)]
if journal_v:
dff = dff[dff.Journal.isin(journal_v)]
if topic_v:
dff = dff[dff.Topic.isin(topic_v)]
text = {word: value for word, value in model_bert.get_topic(topicwc_v)}
wc = WordCloud(background_color="white", max_words=1000)
wc.generate_from_frequencies(text)
wc.to_image()
img = BytesIO()
wc.save(img, format='PNG')
return 'data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode())
return dff.to_dict('records'), row_v, 'data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode())
I think it works, because I am having a new callback error:
Traceback (most recent call last):
File "/var/folders/kj/dkjqkk2n3wq2zfbttgdpjrj80000gn/T/ipykernel_5478/68932548.py", line 272, in update_dropdown_options
wc.save(img, format='PNG')
AttributeError: 'WordCloud' object has no attribute 'save'
As I said in my original question, I have the topics. Each topic contains 15 words and form those the word cloud is generated. The model provides probabilities for the size of the words.
This is what the code ends up as:
def plot_wordcloud(topic):
text = {word: value for word, value in model_bert.get_topic(topic)}
wc = WordCloud(background_color="white", max_words=1000)
wc.generate_from_frequencies(text)
return wc.to_image()
@app.callback(
Output(docs_info_table, 'data'),
Output(docs_info_table, 'page_size'),
Input(year_drop, 'value'),
Input(journal_drop, 'value'),
Input(topic_drop, 'value'),
Input(row_drop, 'value')
)
def update_dropdown_options(year_v, journal_v, topic_v, row_v):
dff = docs_info.copy()
if year_v:
dff = dff[dff.Year.isin(year_v)]
if journal_v:
dff = dff[dff.Journal.isin(journal_v)]
if topic_v:
dff = dff[dff.Topic.isin(topic_v)]
return dff.to_dict('records'), row_v
@app.callback(
Output('image_wc', 'src'),
Input('image_wc', 'id'),
Input('topicwc_drop', 'value'))
def make_image(b, topicwc_drop):
img = BytesIO()
plot_wordcloud(topicwc_drop).save(img, format='PNG')
return 'data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode())