I want to create a Python app where some data is retrieved and charted on a Dash app.
This is the Dash part. This is where my whole data will be sent and shown on a webpage, since i'm starting now, this is just a basic example of a live chart.
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
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'),
events=[Event('graph-update', 'interval')])
def update_graph_scatter():
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(debug=True)
This is the Python part where the data is retrieved, connecting to a Websocket.
import websocket
import json
from bitmex_websocket import Instrument
from bitmex_websocket.constants import InstrumentChannels
from bitmex_websocket.constants import Channels
websocket.enableTrace(True)
channels = [
InstrumentChannels.trade,
]
XBTUSD = Instrument(symbol='XBTUSD',
channels=channels)
XBTUSD.on('action', lambda msg: test(msg))
XBTUSD = Instrument(symbol='XBTUSD',
channels=channels)
XBTUSD.on('action', lambda msg: rekter(msg))
def rekter(msg):
if msg['table'] =='trade':
Rate = msg['data'][0]['price']
print(Rate)
XBTUSD.run_forever()
Right now this second part should only send rate
to the chart, but in the future i'm looking forward to have it processing a lot more data every second, and i would like to work with Pandas and Numpy to process this data.
My problem is that i don't know how to "embed" my second part into the first part. I tried do that, but if i put the lineXBTUSD.run_forever()
before app.run_server(debug=True)
only one part of the code will be executed and my whole Dash app won't run until i stop the first part of the code. Same happens if i do the opposite. Is it possible to have both running at the same time? Or should i just look for another way?