This code works but the same document gets returned multiple times. What supposed to happen is an array of [1,2,3,...] in the x-axis and [1,2,3,4, .....] in the y-axis gets plotted. However, Y is sometimes returned as [1,0,0,1,1,2,3,3,4,5,5,5,].What can I do to fix this? This code works but the same document gets returned multiple times. What supposed to happen is an array of [1,2,3,...] in the x-axis and [1,2,3,4, .....] in the y-axis gets plotted. However, Y is sometimes returned as [1,0,0,1,1,2,3,3,4,5,5,5,].What can I do to fix this?
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
from plotly import graph_objs
from collections import deque # Define max size
import pymongo
import constants
import logging
from collections import OrderedDict
client = pymongo.MongoClient(constants.MONGODB_CONN)
X = []
Y = []
X.append(1)
Y.append(1)
count = 0
resume_token = 'NEW'
token_holder = []
stream = client.changestream.collection.watch()
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():
global stream
global token_holder
global X
global Y
global client
global resume_token
if resume_token is "NEW":
print('NEW DONE NEW DONE NEW DONE NEW DONE')
for insert_change in stream:
resume_token = stream.resume_token
# print('Working 3')
# print(change['fullDocument']['hello'])
# print('Working 4')
print('') # for readability only
X.append(X[-1] + 1)
Y.append(insert_change['fullDocument']['hello'])
print(insert_change['fullDocument']['hello'])
print(X)
# print(Y)
# token_holder.append(resume_token)
data = graph_objs.Scatter(
x=list(X),
y=list(Y),
name='Scatter',
mode='markers'
)
return {'data': [data], 'layout': graph_objs.Layout(xaxis=dict(range=[min(X), max(X)]),
yaxis=dict(range=[min(Y), max(Y)]))}
else:
for insert_change in stream:
# token_holder.append(resume_token)
# print('Working 3')
# print(change['fullDocument']['hello'])
# print('Working 4')
# print('') # for readability only
X.append(X[-1] + 1)
Y.append(insert_change['fullDocument']['hello'])
print(insert_change['fullDocument']['hello'])
print(X)
print(Y)
if resume_token in token_holder:
resume_token = stream.resume_token
token_holder.append(resume_token)
data = graph_objs.Scatter(
x=list(X),
y=list(Y),
name='Scatter',
mode='markers'
)
return {'data': [data], 'layout': graph_objs.Layout(xaxis=dict(range=[min(X), max(X)]),
yaxis=dict(range=[min(Y), max(Y)]))}
if __name__ == "__main__":
app.run_server(debug=True)