在streamlit上更新plotnine图
目标
我想不断更新 streamlit
应用程序上的 plotnine
绘图。
代码
我从 socket
接收数据,然后将其绘制如下:
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))
问题
但是上面的代码(带或不带 st.empty
占位符)生成以下图。我只想保留一个图并想更新其上的点位置。我怎样才能做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为一个想法可以是上传 stramlit 文件,如 How让 Streamlit 每 5 秒重新加载一次?。您可以尝试一个每秒增加的示例(但您可以用您的操作替换添加操作),我将其放入refresher.py中:
现在您可以运行:
现在您可以在streamlit URL上打开浏览器(http://localhost:8501 /) 并单击始终重新运行(这可能需要最新版本的 Streamlit)。您将看到 steamlit 页面的增量自动重新加载。
I think that an idea can be to upload the stramlit file like suggested in How to make Streamlit reloads every 5 seconds?. You can try an example that increase each seconds (but you substitute the add operation with yours), I put it into refresher.py:
Now you can run:
Now you can open your browser on streamlit URL (http://localhost:8501/) and click on Always rerun (this need the newest version of streamlit maybe). You will see the increment autoreload the steamlit page.