Start a simple zmq data publishing service, as a demo:
%%writefile publisher.py
import time
import pickle
import numpy as np
import zmq
size = 100
scale = 100
url = 'tcp://127.0.0.1:5555'
def main():
ctx = zmq.Context.instance()
pub = ctx.socket(zmq.PUB)
pub.bind(url)
while True:
data = np.cumsum(np.random.randn(size) * scale)
md = {
'shape': data.shape,
'dtype': data.dtype,
}
pub.send_multipart([
pickle.dumps(md, pickle.HIGHEST_PROTOCOL),
data,
])
time.sleep(1)
if __name__ == '__main__':
main()
Overwriting publisher.py
from subprocess import Popen
publisher = Popen([sys.executable, 'publisher.py'])
Make an ipywidgets-based plot with bqplot:
import numpy as np
import bqplot as bq
size = 100
scale = 100.
np.random.seed(0)
x_data = np.arange(size)
y_data = np.cumsum(np.random.randn(size) * scale)
x_sc = bq.LinearScale()
y_sc = bq.LinearScale()
ax_x = bq.Axis(label='X', scale=x_sc, grid_lines='solid')
ax_y = bq.Axis(label='Y', scale=y_sc, orientation='vertical', grid_lines='solid')
line = bq.Lines(x=x_data, y=y_data, scales={'x': x_sc, 'y': y_sc})
fig = bq.Figure(axes=[ax_x, ax_y], marks=[line], title='Live Updates!')
fig
Subscribe to our data-publisher, updating
import pickle
def subscribe(url):
ctx = zmq.Context.instance()
s = ctx.socket(zmq.SUB)
s.connect('tcp://127.0.0.1:5555')
s.subscribe = b''
while True:
pmd, buf = s.recv_multipart()
md = pickle.loads(pmd)
a = np.frombuffer(buf, dtype=md['dtype']).reshape(md['shape'])
# setting line.y updates the plot above
line.y = a
from threading import Thread
subscriber = Thread(target=subscribe, args=('tcp://127.0.0.1:5555',))
subscriber.start()