Goal
I want to continuously update a plotnine
plot on a streamlit
app.
Code
I receive data from socket
and then plot it as follows:
import socket
import struct
import pandas as pd
import streamlit as st
import time
from plotnine import *
UDP_IP = "127.0.0.1"
UDP_PORT = 9000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
time.sleep(0.1)
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
fields = struct.unpack_from('=ddd', data)
d = {'y': fields[0],
'x': fields[1],
'z': fields[2]}
dff = pd.DataFrame([d], columns=d.keys())
# creating a single-element container.
# placeholder = st.empty()
# with placeholder.container():
st.markdown("### First Chart")
fig = ggplot(dff, aes("x", "y")) + geom_point()
st.pyplot(ggplot.draw(fig))
Problem
But the code above (with or without st.empty
placeholder) produces the following plot. I want to keep just one plot and want to update the point position on it. How can I achieve that?