我对Dash相当陌生-Plotly,我正在尝试开发一个实时的Twitter情绪仪表板。我有一个用于实时Twitter情绪的图表和一个搜索栏,用户可以在其中输入术语并查看该术语的实时情绪。我现在尝试在初始图表下方添加一个总情感/每学期情感(即“ covid”的情感)的饼图。
正在从AWS RDS上的MySql数据库中查询饼图的数据,而由于Heroku每24小时会自动删除一次,因此要在SQLite中查询实时图。
这是我的app.layout
app.layout = html.Div(
children=[
html.Div(className='row',
children=[
html.Div(className='four columns div-user-controls',
children=[
html.H2('Twitter Sentiment Dashboard'),
html.P('Visulaizing live twitter sentiment with Plotly - Dash.'),
html.P(
'Type a term in the search box below that you would like to see the sentiment from.'),
html.Div(
className='div-for-search',
children=[
dcc.Input(
id='sentiment_term',
placeholder='Enter a term...',
type='text',
value='',
className='search'
),
],
style={'color': ' #1E1E1E'})
]
),
html.Div(className='eight columns div-for-chars bg-grey',
children=[
dcc.Graph(id='live-graph', config={'displayModeBar': False}, animate=False),
dcc.Interval(
id='graph-update',
interval=1 * 1000
),
dcc.Graph(id='pie'),
dcc.Interval(
id='pie-update',
interval=1 * 1000
)
])
])
]
)
这是我的饼图功能
@app.callback(Output('pie', 'figure'),
[Input(component_id='sentiment_term', component_property='value')],
events=[Event('pie-update', 'interval')])
def update_pie(sentiment_term):
try:
db_connection = sql.connect(host=database_endpoint_url, database=database_name, user=database_user,
password=database_password)
db_cursor = db_connection.cursor()
df = pd.read_sql("SELECT * FROM tweets WHERE text LIKE ?", con=db_connection,
params=('%' + sentiment_term + '%',))
sentiments = df['sentiment'].tolist()
positive = 0
neutral = 0
negative = 0
for sentiment in sentiments:
sentiment = float(sentiment)
if sentiment < -0.2:
negative += 1
if sentiment > 0.2:
positive += 1
else:
neutral += 1
values = [positive, negative, neutral]
labels = ['Positive', 'Negative', 'Mixed']
trace = go.Pie(labels=labels, values=values,
hoverinfo='label+percent', textinfo='value',
textfont=dict(size=20, color=app_colors['text']),
marker=dict(
line=dict(color=app_colors['background'], width=2)))
return {"data": [trace], 'layout': go.Layout(
font={'color': app_colors['text']},
plot_bgcolor=app_colors['background'],
paper_bgcolor=app_colors['background'],
showlegend=True)}
我得到的是白色x / y图表,没有我的饼图应该存在的值。知道为什么我没有得到饼图吗?我也知道,仅在我遵循的教程中使用了事件,事件才被弃用,并且一切正常,一旦我更改为仅输入,就会出现一些错误。