【问题标题】:Dash- Plotly null is not an object (evaluating 'figure.data')Dash-Plotly null 不是对象(评估“figure.data”)
【发布时间】:2019-06-22 01:55:25
【问题描述】:

在这里需要一些帮助。有一个 Flask 应用程序会触发此错误,但不知道如何改进它。任何帮助深表感谢!似乎情节应用程序如何使用反应并将事物渲染到DOM中的方式存在一些错误。我正在做的是将一些数据保存到数据库中,并从数据库中创建一个图表,该图表显示了一个索引,该索引必须由应用程序相应地呈现到 DOM 中。

这是整个错误:

TypeError: null is not an object (evaluating 'figure.data')
plot — Graph.react.js:108
componentWillReceiveProps — Graph.react.js:230
Zf — react-dom@16.8.6.min.js:67:396
qg — react-dom@16.8.6.min.js:95:339
Qg — react-dom@16.8.6.min.js:144:296
Rg — react-dom@16.8.6.min.js:145:171
Sc — react-dom@16.8.6.min.js:158:112
Z — react-dom@16.8.6.min.js:156:495
Kc — react-dom@16.8.6.min.js:155
ya — react-dom@16.8.6.min.js:153:188
enqueueSetState — react-dom@16.8.6.min.js:202:412
setState — react@16.8.6.min.js:20:449
handleChange — connect.js:302
handleChange
dispatch — createStore.js:173
handleResponse — index.js:742
handleJson — index.js:913
promiseReactionJob

这是我的 python 烧瓶代码

import dash
from dash.dependencies import Output, Input
#Event
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
import time
#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='olympic', 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')],
            #   events=[Event('graph-update', 'interval')]
            )
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 200", conn ,params=('%' + sentiment_term + '%',))
        df.sort_values('unix', inplace=True)
        df['sentiment_smoothed'] = df['sentiment'].rolling(int(len(df)/2)).mean()

        df['date'] = pd.to_datetime(df['unix'],unit='ms')
        df.set_index('date', inplace=True)
        df = df.resample('1min').mean()
        df.dropna(inplace=True)
        X = df.index
        Y = df.sentiment_smoothed

        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)

【问题讨论】:

    标签: flask plotly-dash


    【解决方案1】:

    我替换了一个虚拟数据框,因为我无权访问您的数据库。代码运行并使用我的数据框正确显示图形,所以我强烈怀疑 ETL 代码有问题。

    您的错误消息报告figure 属性的data 部分是null,这也让我认为ETL 过程出现了问题。尝试沿途检查每个步骤的内容,看看是否在某个时候最终会在某处获得 None 值。

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 2022-01-05
      • 2021-10-20
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多