如何在 dash 网络应用程序上显示 wordcloud 图像
how to show wordcloud image on dash web application
我需要在我的 dash 应用程序上呈现一个文字云。根据这个帖子 https://community.plot.ly/t/solved-is-it-possible-to-make-a-wordcloud-in-dash/4565,dash 中没有 wordcloud 内置组件。一种解决方法是使用 WordCloud
模块将词云生成为图像并使用 dash_html_components.Img
在布局上显示。
我是达世币的新手。不确定如何渲染图像。每次制作词云时都需要将词云保存为临时图像吗?
如果任何具有 Dash 专业知识的人可以提供帮助,我将不胜感激。
代码如下:
import dash
import dash_core_components as dcc
import dash_html_components as html
print(dcc.__version__) # 0.6.0 or above is required
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
dfm = pd.DataFrame({'word': ['apple', 'pear', 'orange'], 'freq': [1,3,9]})
app.layout = html.Div([
html.Img(id = 'image_wc')
])
# function to make wordcoud from word frequency dataframe
def plot_wordcloud (data):
d = {}
for a, x in data.values:
d[a] = x
wc = WordCloud(background_color='black',
width=1800,
height=1400).generate_from_frequencies(frequencies=d)
return (wc)
@app.callback(dash.dependencies.Output('image_wc', 'img'))
def make_image ():
img = plot_wordcloud (data = dfm)
return (img)
if __name__ == '__main__':
app.run_server(debug=True)
下面是一个工作示例。它使用 pip
-可安装的 wordcloud
库,并通过 BytesIO
传递结果图像的 base-64 编码 PNG 表示形式,因此您不需要每次都转储所有创建的 PNG 文件时间.
我让它自己触发,所以当你加载 Dash 应用程序时它会 运行,尽管你可以让它通过按钮或类似的东西动态地工作。
import dash
import dash.dependencies as dd
import dash_core_components as dcc
import dash_html_components as html
from io import BytesIO
import pandas as pd
from wordcloud import WordCloud
import base64
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__) #, external_stylesheets=external_stylesheets)
dfm = pd.DataFrame({'word': ['apple', 'pear', 'orange'], 'freq': [1,3,9]})
app.layout = html.Div([
html.Img(id="image_wc"),
])
def plot_wordcloud(data):
d = {a: x for a, x in data.values}
wc = WordCloud(background_color='black', width=480, height=360)
wc.fit_words(d)
return wc.to_image()
@app.callback(dd.Output('image_wc', 'src'), [dd.Input('image_wc', 'id')])
def make_image(b):
img = BytesIO()
plot_wordcloud(data=dfm).save(img, format='PNG')
return 'data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode())
if __name__ == '__main__':
app.run_server(debug=True)
我需要在我的 dash 应用程序上呈现一个文字云。根据这个帖子 https://community.plot.ly/t/solved-is-it-possible-to-make-a-wordcloud-in-dash/4565,dash 中没有 wordcloud 内置组件。一种解决方法是使用 WordCloud
模块将词云生成为图像并使用 dash_html_components.Img
在布局上显示。
我是达世币的新手。不确定如何渲染图像。每次制作词云时都需要将词云保存为临时图像吗?
如果任何具有 Dash 专业知识的人可以提供帮助,我将不胜感激。
代码如下:
import dash
import dash_core_components as dcc
import dash_html_components as html
print(dcc.__version__) # 0.6.0 or above is required
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
dfm = pd.DataFrame({'word': ['apple', 'pear', 'orange'], 'freq': [1,3,9]})
app.layout = html.Div([
html.Img(id = 'image_wc')
])
# function to make wordcoud from word frequency dataframe
def plot_wordcloud (data):
d = {}
for a, x in data.values:
d[a] = x
wc = WordCloud(background_color='black',
width=1800,
height=1400).generate_from_frequencies(frequencies=d)
return (wc)
@app.callback(dash.dependencies.Output('image_wc', 'img'))
def make_image ():
img = plot_wordcloud (data = dfm)
return (img)
if __name__ == '__main__':
app.run_server(debug=True)
下面是一个工作示例。它使用 pip
-可安装的 wordcloud
库,并通过 BytesIO
传递结果图像的 base-64 编码 PNG 表示形式,因此您不需要每次都转储所有创建的 PNG 文件时间.
我让它自己触发,所以当你加载 Dash 应用程序时它会 运行,尽管你可以让它通过按钮或类似的东西动态地工作。
import dash
import dash.dependencies as dd
import dash_core_components as dcc
import dash_html_components as html
from io import BytesIO
import pandas as pd
from wordcloud import WordCloud
import base64
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__) #, external_stylesheets=external_stylesheets)
dfm = pd.DataFrame({'word': ['apple', 'pear', 'orange'], 'freq': [1,3,9]})
app.layout = html.Div([
html.Img(id="image_wc"),
])
def plot_wordcloud(data):
d = {a: x for a, x in data.values}
wc = WordCloud(background_color='black', width=480, height=360)
wc.fit_words(d)
return wc.to_image()
@app.callback(dd.Output('image_wc', 'src'), [dd.Input('image_wc', 'id')])
def make_image(b):
img = BytesIO()
plot_wordcloud(data=dfm).save(img, format='PNG')
return 'data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode())
if __name__ == '__main__':
app.run_server(debug=True)