Reputation: 47
I have taken the following code from https://github.com/coderspage/flask-sse.
I am trying to render dictionary continuously from the backend.
I know how to render dictionary using jinja template engine, but don't know how can I render it here.
can anyone tell me how to do this?
server.py
@app.route("/listen")
def listen():
def respond_to_client():
while True:
df = pd.read_csv(filename)
df['link_tuple'] = list(zip(df.fut_link, df.option_link))
final_dict = pd.Series(df.link_tuple.values, index=df.symbol).to_dict()
_data = json.dumps(final_dictionary)
yield f"id: 1\ndata: {_data}\nevent: online\n\n"
time.sleep(0.5)
return Response(respond_to_client(), mimetype='text/event-stream')
.
index.html
<body>
// render data object here
<script>
var eventSource = new EventSource("/listen")
eventSource.addEventListener("message", function(e) {
console.log(e.data)
}, false)
eventSource.addEventListener("online", function(e) {
let data = JSON.parse(e.data)
}, true)
</script>
</body>
.
data object
{
"ASIANPAINT24JUN21FUT": [
"https://in.tradingview.com/chart/?symbol=NSE:ASIANPAINT1!",
"https://kite.zerodha.com/chart/ext/tvc/NFO-OPT/ASIANPAINT21JUN2980CE/15044354"
],
"DEEPAKNTR24JUN21FUT": [
"https://in.tradingview.com/chart/?symbol=NSE:DEEPAKNTR1!",
"https://kite.zerodha.com/chart/ext/tvc/NFO-OPT/DEEPAKNTR21JUN1820CE/20372738"
],
"DRREDDY24JUN21FUT": [
"https://in.tradingview.com/chart/?symbol=NSE:DRREDDY1!",
"https://kite.zerodha.com/chart/ext/tvc/NFO-OPT/DRREDDY21JUN5350CE/20732162"
],
"ESCORTS24JUN21FUT": [
"https://in.tradingview.com/chart/?symbol=NSE:ESCORTS1!",
"https://kite.zerodha.com/chart/ext/tvc/NFO-OPT/ESCORTS21JUN1220CE/20892674"
]
}
Upvotes: 0
Views: 204