【问题标题】:Cannot read properly 'data ' of null (DASH)无法读取 null (DASH) 的属性“数据”
【发布时间】:2020-06-03 21:41:51
【问题描述】:

当我运行以下代码时,它会加载网页,但随后会留下错误消息,这是因为在某些部分没有数据,条件可以帮助吗?

我上传了代码,但我不知道在哪里行动,我是 Dash 的新手,我对 javascript 的了解有限。

我的主文件

import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
from collections import deque
import sqlite3
import pandas as pd

#popular topics: google, olympics, trump, gun, usa

app = dash.Dash(__name__)
app.layout = html.Div(
    [   html.H2('Live Twitter Sentiment'),
        dcc.Input(id='sentiment_term', value='trump', type='text'),
        dcc.Graph(id='live-graph', animate=False),
        dcc.Interval(
            id='graph-update',
            interval=1*1000
        ),
    ]
)


@app.callback(Output('live-graph', 'figure'),
                   [Input(component_id='sentiment_term', component_property='value')])


def update_graph_scatter(sentiment_term):
    try:
        conn = sqlite3.connect('twitter.db')
        c = conn.cursor()
        df = pd.read_sql("SELECT * FROM sentiment WHERE tweet LIKE ? ORDER BY unix DESC LIMIT 1000", conn, params=('%' + sentiment_term + '%',))
        df.sort_values('unix', inplace=True)
        df['sentiment_smoothed'] = df['sentiment'].rolling(int(len(df)/5)).mean()
        df.dropna(inplace=True)

        X = df.unix.values[-100:]
        Y = df.sentiment_smoothed.values[-100:]

        data = plotly.graph_objs.Scatter(
                x=X,
                y=Y,
                name='Scatter',
                mode= 'lines+markers'
                )

        return {'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
                                                    yaxis=dict(range=[min(Y),max(Y)]),
                                                    title='Term: {}'.format(sentiment_term))}

    except Exception as e:
        with open('errors.txt','a') as f:
            f.write(str(e))
            f.write('\n')


if __name__ == '__main__':
    app.run_server(debug=True)

【问题讨论】:

  • 我们可能需要查看完整的堆栈跟踪,但我有一个猜测。尝试在您的dcc.Graph 中设置figure={}。如果您要更新的道具未初始化,有时 Dash 不喜欢它。
  • 你好@coralvanda你解决了我的问题!!天才,谢谢,如果你想在下面添加答案,我可以验证它

标签: python python-3.x typescript flask plotly-dash


【解决方案1】:

如果回调更新的道具尚未初始化,有时 Dash 可能会遇到困难。在这种情况下,dcc.Graphfigure 属性从未被声明过。设置一个显式的空值,例如figure={} 通常足以解决此类错误。

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多