在streamlit上更新plotnine图

发布于 2025-01-15 08:15:05 字数 1176 浏览 4 评论 0原文

目标

我想不断更新 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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-01-22 08:15:05

我认为一个想法可以是上传 stramlit 文件,如 How让 Streamlit 每 5 秒重新加载一次?。您可以尝试一个每秒增加的示例(但您可以用您的操作替换添加操作),我将其放入refresher.py中:

from random import randint
import time
import os
def refresher(seconds):
    x = 0
    while True:
        mainDir = os.path.dirname(__file__)
        filePath = os.path.join(mainDir, 'test.py')
        x += 1
        # write (or upload) your streamlit file
        with open(filePath, 'w') as f:
            f.write(f'import streamlit as st\nst.title("# {x}")')
        time.sleep(seconds)
refresher(1)

现在您可以运行:

# terminal 1
python refresher.py
# terminal 2
streamlit run test.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:

from random import randint
import time
import os
def refresher(seconds):
    x = 0
    while True:
        mainDir = os.path.dirname(__file__)
        filePath = os.path.join(mainDir, 'test.py')
        x += 1
        # write (or upload) your streamlit file
        with open(filePath, 'w') as f:
            f.write(f'import streamlit as st\nst.title("# {x}")')
        time.sleep(seconds)
refresher(1)

Now you can run:

# terminal 1
python refresher.py
# terminal 2
streamlit run test.py

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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文