【发布时间】:2019-11-18 04:08:12
【问题描述】:
我需要在我的 dash 应用程序上渲染一个 wordcloud。根据这个线程https://community.plot.ly/t/solved-is-it-possible-to-make-a-wordcloud-in-dash/4565,dash 中没有 wordcloud 内置组件。一种解决方法是使用WordCloud 模块将wordcloud 生成为图像,并使用dash_html_components.Img 在布局上显示。
我是 Dash 的新手。不知道如何渲染图像。每次生成 wordcloud 时都需要将 wordcloud 保存为临时图像吗?
如果有 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)
【问题讨论】:
标签: python plotly-dash