Black Lives Matter. Please consider donating to Black Girls Code today.

Populating dash with data from udp thread

Hi i’m trying to update some plotly graphs with data received from udp thread. Basically ’ temp = rx.dataRxGraph’ in the update_graph_scatter() always returns blank. Any ideas?

import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
from random import random
import plotly
import socket
from time import sleep
import threading

class dataRxObj():
dataRxGraph = []

def socketThread():
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data
    rx.dataRxGraph.append(int(data))

app = dash.Dash(name)
app.layout = html.Div(
html.Div([

    dcc.Graph(id='live-update-graph-scatter', animate=True),
    dcc.Interval(
        id='interval-component',
        interval=1*500
    )
])

)

@app.callback(Output(‘live-update-graph-scatter’, ‘figure’),
events=[Event(‘interval-component’, ‘interval’)])
def update_graph_scatter():

temp = rx.dataRxGraph
x = []
for i in range(0,len(temp)):
    x.append(i)

traces = list()
for t in range(2):
    traces.append(plotly.graph_objs.Scatter(
        x=x,
        y=temp, #y axis
        name='Scatter {}'.format(t),
        mode= 'lines+markers'
        ))
return {'data': traces}

if name == ‘main’:
rx = dataRxObj()
threads = []
td = threading.Thread(target=socketThread)
threads.append(td)
td.start()
app.run_server(debug=True)

Can you get data from the socket thing outside of the context of a Dash callback? If so, I reckon StackOverflow would be a better place to ask this question, since it might be that your problem has more to do with using sockets than Dash per se.