Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ImportError: cannot import name 'Event' in Dash from plotly (python)?

I am working on data visualization task in which used Dash from plotly(python). When I am running the code I got an error which is -

ImportError: cannot import name 'Event'

I have tried various installation processes like pip install events or pip install Event, but I am not able to solve my error.

code:

import dash
from dash.dependencies import Output, 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

x = deque(maxlen=20)
y = deque(maxlen=20)

x.append(1)
y.append(1)

app = dash.Dash(__name__)

app.layout = html.Div(
    [ 
        dcc.Graph(id = 'live-graph', animate = True),
        dcc.Interval(
                id = 'graph-update',
                    interval = 1000
                )

        ]
    )   

@app.callback(Output('live-graph','figure'),
                events = [Event('graph-update','interval')])

def update_graph():
    
    globalx
    globaly
    
    x.append(x[-1]+1)
    y.append(y[-1]+(y[-1]*random.uniform(-0.1,0.1)))

    data = go.Scatter(
           
            x = list (x),
            y = list(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)]))}                                                        
                                        

if __name__ == '__main__':
    app.run_server(debug = True, port = 8051) 
like image 665
chirag mehta Avatar asked Feb 21 '19 13:02

chirag mehta


People also ask

Is Plotly the same as Dash?

Dash is a python framework created by plotly for creating interactive web applications. Dash is written on the top of Flask, Plotly. js and React.

Is dash Plotly free for commercial use?

Is Dash free? Yes. Plotly's Dash analytics application framework is also free and open-source software, licensed under the MIT license.

What is App callback?

A callback is a function that executes when a user interacts with a UI component in your app. You can use callbacks to program the behavior of your app.

What is Dash Python?

Dash is Python framework for building web applications. It built on top of Flask, Plotly. js, React and React Js. It enables you to build dashboards using pure Python. Dash is open source, and its apps run on the web browser.


2 Answers

To preserve using the latest Dash version and keep updated you can use following code to fix the problem:

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

X = deque(maxlen=20)
X.append(1)
Y = deque(maxlen=20)
Y.append(1)


app = dash.Dash(__name__)
app.layout = html.Div(
    [
        dcc.Graph(id='live-graph', animate=True),
        dcc.Interval(
            id='graph-update',
            interval=1*1000
        ),
    ]
)

@app.callback(Output('live-graph', 'figure'),
              [Input('graph-update', 'n_intervals')])
def update_graph_scatter(input_data):
    X.append(X[-1]+1)
    Y.append(Y[-1]+Y[-1]*random.uniform(-0.1,0.1))

    data = plotly.graph_objs.Scatter(
            x=list(X),
            y=list(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)]),)}


if __name__ == '__main__':
    app.run_server(host='0.0.0.0', port=8080 ,debug=True)

The change in this code is just in the callback. The word n_intervals is the new way of Dash to handle events. As the name suggests, n_intervals keeps count of how many times the interval has fired, and so each time the interval fires, n_intervals gets incremented which triggers your callback. The only change you have to make to the callback is to an argument to receive n_intervals, which you can then ignore in the function body.

like image 138
casjorge Avatar answered Sep 28 '22 17:09

casjorge


Event was removed in the latest version (0.37) of Dash, that's why you cannot import it. See dev comment.

If you're bent on using it, switch to 0.36, but I'd not recommend that.

like image 37
Frenzy Kiwi Avatar answered Sep 28 '22 17:09

Frenzy Kiwi