Sentdex / socialsentiment

Sentiment Analysis application created with Python and Dash, hosted at socialsentiment.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot import name 'Event' from 'dash.dependencies'

krispykres opened this issue · comments

Looks like the Event system got removed in the latest version of Dash. Is there a reference to know what versions of packages were installed at the time this tutorial was being created?

dash==0.30.0
dash-core-components==0.38.0
dash-html-components==0.13.2
dash-renderer==0.15.0

They say the Events got removed with dash 0.37, leaving 0.36 the latest version to support it. But the latest version I was able to install without getting that error is 0.35

Use Input instead (change interval to n_intervals):
@app.callback(Output('historical-graph', 'figure'), [Input(component_id='sentiment_term', component_property='value'), Input('historical-update', 'n_intervals')])

Also add n_intervals to the Interval:
dcc.Interval( id='sentiment-pie-update', interval=60*1000, n_intervals=0 ),

Use Input instead (change interval to n_intervals):
@app.callback(Output('historical-graph', 'figure'), [Input(component_id='sentiment_term', component_property='value'), Input('historical-update', 'n_intervals')])

Also add n_intervals to the Interval:
dcc.Interval( id='sentiment-pie-update', interval=60*1000, n_intervals=0 ),

Tried fix and dose not work:

CODE:
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,
n_intervals= 0
),

]

)

@app.callback(Output('live-graph', 'figure'),
[Input(component_id='sentiment_term', component_property='value'),
Input('graph-update', 'n_intervals')])
def update_graph_scatter(sentiment_term):``

-- Won't let me format code in comment --

results in error: Callback error updating live-graph.figure

Use Input instead (change interval to n_intervals):
@app.callback(Output('historical-graph', 'figure'), [Input(component_id='sentiment_term', component_property='value'), Input('historical-update', 'n_intervals')])
Also add n_intervals to the Interval:
dcc.Interval( id='sentiment-pie-update', interval=60*1000, n_intervals=0 ),

Tried fix and dose not work:

CODE:
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,
n_intervals= 0
),

]

)

@app.callback(Output('live-graph', 'figure'),
[Input(component_id='sentiment_term', component_property='value'),
Input('graph-update', 'n_intervals')])
def update_graph_scatter(sentiment_term):``

-- Won't let me format code in comment --

results in error: Callback error updating live-graph.figure

Can't remember what I did but this is the code for the functions update_graph_scatter and update_hist_graph_scatter.

`
@app.callback(Output('live-graph', 'figure'),
[Input('sentiment_term', 'value'), Input('graph-update', 'n_intervals')])
def update_graph_scatter(sentiment_term, n_intervals):
try:
if sentiment_term:
df = pd.read_sql("SELECT sentiment.* FROM sentiment_fts fts LEFT JOIN sentiment ON fts.rowid = sentiment.id WHERE fts.sentiment_fts MATCH ? ORDER BY fts.rowid DESC LIMIT 1000", conn, params=(sentiment_term+'*',))
else:
df = pd.read_sql("SELECT * FROM sentiment ORDER BY id DESC, unix DESC LIMIT 1000", conn)
df.sort_values('unix', inplace=True)
df['date'] = pd.to_datetime(df['unix'], unit='ms')
df.set_index('date', inplace=True)
init_length = len(df)
df['sentiment_smoothed'] = df['sentiment'].rolling(int(len(df)/5)).mean()
df = df_resample_sizes(df)
X = df.index
Y = df.sentiment_smoothed.values
Y2 = df.volume.values
data = plotly.graph_objs.Scatter(
x=X,
y=Y,
name='Sentiment',
mode= 'lines',
yaxis='y2',
line = dict(color = (app_colors['sentiment-plot']),
width = 4,)
)

    data2 = plotly.graph_objs.Bar(
            x=X,
            y=Y2,
            name='Volume',
            marker=dict(color=app_colors['volume-bar']),
            )

    return {'data': [data,data2],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
                                                      yaxis=dict(range=[min(Y2),max(Y2*4)], title='Volume', side='right'),
                                                      yaxis2=dict(range=[min(Y),max(Y)], side='left', overlaying='y',title='sentiment'),
                                                      title='Live sentiment for: "{}"'.format(sentiment_term),
                                                      font={'color':app_colors['text']},
                                                      plot_bgcolor = app_colors['background'],
                                                      paper_bgcolor = app_colors['background'],
                                                      showlegend=False)}

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

@app.callback(Output('historical-graph', 'figure'),
[Input(component_id='sentiment_term', component_property='value'),
Input('historical-update', 'n_intervals')])
def update_hist_graph_scatter(sentiment_term, n_intervals):
try:
if sentiment_term:
df = pd.read_sql("SELECT sentiment.* FROM sentiment_fts fts LEFT JOIN sentiment ON fts.rowid = sentiment.id WHERE fts.sentiment_fts MATCH ? ORDER BY fts.rowid DESC LIMIT 10000", conn, params=(sentiment_term+'*',))
else:
df = pd.read_sql("SELECT * FROM sentiment ORDER BY id DESC, unix DESC LIMIT 10000", conn)
df.sort_values('unix', inplace=True)
df['date'] = pd.to_datetime(df['unix'], unit='ms')
df.set_index('date', inplace=True)
# save this to a file, then have another function that
# updates because of this, using intervals to read the file.
# https://community.plot.ly/t/multiple-outputs-from-single-input-with-one-callback/4970

    # store related sentiments in cache
    cache.set('related_terms', sentiment_term, related_sentiments(df, sentiment_term), 120)

    # print(related_sentiments(df,sentiment_term), sentiment_term)
    init_length = len(df)
    df['sentiment_smoothed'] = df['sentiment'].rolling(int(len(df)/5)).mean()
    df.dropna(inplace=True)
    df = df_resample_sizes(df,maxlen=500)
    X = df.index
    Y = df.sentiment_smoothed.values
    Y2 = df.volume.values

    data = plotly.graph_objs.Scatter(
            x=X,
            y=Y,
            name='Sentiment',
            mode= 'lines',
            yaxis='y2',
            line = dict(color = (app_colors['sentiment-plot']),
                        width = 4,)
            )

    data2 = plotly.graph_objs.Bar(
            x=X,
            y=Y2,
            name='Volume',
            marker=dict(color=app_colors['volume-bar']),
            )

    df['sentiment_shares'] = list(map(pos_neg_neutral, df['sentiment']))

    # sentiment_shares = dict(df['sentiment_shares'].value_counts())
    cache.set('sentiment_shares', sentiment_term, dict(df['sentiment_shares'].value_counts()), 120)

    return {'data': [data,data2],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]), # add type='category to remove gaps'
                                                      yaxis=dict(range=[min(Y2),max(Y2*4)], title='Volume', side='right'),
                                                      yaxis2=dict(range=[min(Y),max(Y)], side='left', overlaying='y',title='sentiment'),
                                                      title='Longer-term sentiment for: "{}"'.format(sentiment_term),
                                                      font={'color':app_colors['text']},
                                                      plot_bgcolor = app_colors['background'],
                                                      paper_bgcolor = app_colors['background'],
                                                      showlegend=False)}

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

max_size_change = .4
`

Use Input instead (change interval to n_intervals):
@app.callback(Output('historical-graph', 'figure'), [Input(component_id='sentiment_term', component_property='value'), Input('historical-update', 'n_intervals')])
Also add n_intervals to the Interval:
dcc.Interval( id='sentiment-pie-update', interval=60*1000, n_intervals=0 ),

Tried fix and dose not work:

CODE:
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,
n_intervals= 0
),

]

)

@app.callback(Output('live-graph', 'figure'),
[Input(component_id='sentiment_term', component_property='value'),
Input('graph-update', 'n_intervals')])
def update_graph_scatter(sentiment_term):``

-- Won't let me format code in comment --

results in error: Callback error updating live-graph.figure

def update_graph_scatter(sentiment_term) <--- believe you need a 2nd parm here due to the 2 inputs on the callback. Can be called anything as your code doesn't need to use it. Also used dash.dependencies.Input rather than Input for the graph update.

Like this:
@app.callback(Output('live-graph', 'figure'),
[dash.dependencies.Input(component_id='sentiment_term', component_property='value'),
dash.dependencies.Input('graph-update', 'n_intervals')])
def update_graph_scatter(sentiment_term, interval):