[英]Streamlit image not being updated
我对 Python 世界和 Streamlit 很陌生,并尝试使用 Streamlit 设置一个小应用程序,该应用程序连接到 websocket 并更新每条消息的图像。 我实现了以下两个文件:
import websockets
import asyncio
import streamlit as st
from PIL import Image
from util import consumer
st.set_page_config(page_title="stream", layout="wide")
status = st.empty()
connect = st.checkbox("Connect to WS Server")
placeholder_image = Image.open('placeholder.png')
image = st.image(placeholder_image)
if connect:
asyncio.run(consumer(status, image))
else:
status.subheader(f"Disconnected.")
和 util 文件:
import io
import websockets
import json
import base64
from PIL import Image
import streamlit as st
WS_CONN = "ws://localhost:8765"
async def consumer(status, image):
websocket = await websockets.connect("ws://localhost:8765")
status.subheader(f"Connected to websocket!")
async for message in websocket:
current_image = Image.open(io.BytesIO(base64.b64decode(json.loads(message)["image"]))) # until here it works fine, sample log is <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720 at 0x129D81FD0>
image.image = current_image # image is not being updated
当前的问题是,当我尝试在 websocket 消息处理程序中设置图像时,图像没有被更新。 我在这里的 Streamlit API 中做错了什么吗? 也许是一些缓存问题?
您在应用程序中绘制图像的唯一位置是以下几行:
placeholder_image = Image.open('placeholder.png')
image = st.image(placeholder_image
据我所知,图像placeholder.png
没有更新。
请做st.image(image_from_consumer)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.