Streamlit 应用程序:同时显示来自同一文件夹的多个 gif

问题描述 投票:0回答:1

我想在 Streamlit 应用程序中显示来自同一文件夹的多个 gif。 以下代码包可以编译,但根本不显示 nethin。

import streamlit as st
import base64
import os
import platform
sys = platform.system()
work_dir = os.path.dirname(os.path.abspath(__file__))

if sys == "Windows":
    mypath = work_dir + "\\forecast\\fr\\"
else:
    mypath = work_dir + "/forecast/fr/"

files = [f for f in os.listdir(mypath) if os.path.isfile(f)]
"""### gif from local file"""

for file in files:

    file_ = open(mypath + file, "rb")
    contents = file_.read()
    data_url = base64.b64encode(contents).decode("utf-8")
    file_.close()

    st.markdown(
        f'<img src="data:image/gif;base64,{data_url}" alt="cat gif">',
        unsafe_allow_html=True,
    )
python gif streamlit cartopy
1个回答
0
投票

这解决了问题,使用 glob 更加用户友好:

import streamlit as st
import base64
import os, glob
import platform
sys = platform.system()
work_dir = os.path.dirname(os.path.abspath(__file__))
print(sys)
if sys == "Windows":
    mypath = work_dir + "\\forecast\\fr\\"
else:
    mypath = work_dir + "/forecast/fr/"

print(mypath)
os.chdir(mypath)
fileslist = []
for file in glob.glob("*.gif"):
    print(file)
    fileslist.append(file)

print(fileslist)

for i in range(0,len(fileslist)):
    file_ = open(mypath + fileslist[i], "rb")
    contents = file_.read()
    data_url = base64.b64encode(contents).decode("utf-8")
    file_.close()

    st.markdown(
        f'<img src="data:image/gif;base64,{data_url}" alt="cat gif">',
        unsafe_allow_html=True,
    )
© www.soinside.com 2019 - 2024. All rights reserved.