Real-time data streaming and plot updating

I try to understand if and how steramlit can be used to read data from a feed and display say the latest 10 min data in a plot. For data source I like to use cryptofeed, which is pretty cool but I did not find any reasonable starting point, neither in the doc nor here. My simple minded approach to add new data in a handler fails as there is no current event loop in thread ‘ScriptRunner.scriptThread’

import pandas as pd
import streamlit as st
import numpy as np

from decimal import Decimal
from collections import deque

from cryptofeed import FeedHandler
from cryptofeed.callback import BookCallback, FundingCallback, TickerCallback, TradeCallback
from cryptofeed.defines import BID, ASK, BLOCKCHAIN, COINBASE, FUNDING, GEMINI, L2_BOOK, OPEN_INTEREST, TICKER, TRADES, VOLUME
from cryptofeed.exchanges import Coinbase

prices = deque(maxlen=10)

chart = st.line_chart(np.array([[0.0]]))

async def trade(feed, pair, order_id, timestamp, side, amount, price, receipt_timestamp):
    prices.append(price)
    print(f"Timestamp: {timestamp} rts {receipt_timestamp} Feed: {feed} Pair: {pair} ID: {order_id} Side: {side} Amount: {amount} Price: {price}")
    chart.add_rows(np.array([[price]]))

def handler():
    f = FeedHandler()
    f.add_feed(Coinbase(pairs=['BTC-USD'], channels=[TRADES], callbacks={TRADES: TradeCallback(trade)}))
    f.run()


handler()

Any ideas or suggestions?

Dan

Adding Redis as an intermediate in mem DB allows me to get to a solution with polling. I also benchmarked against Dash, and Streamlit was disappointing. For the time being going on with Dash and see how Streamlit improves. Another benefit of using Dash is I can also merge it with Flask and do direct web socket connection to the frontend. Something I have no clue how to do with Streamlit as the doc is not very detailed.

1 Like

Hi Daniel,

I am trying to do that, if you do not mind… Could you share your code for Dash ?

Thank you so much