我有streamlit的应用程序,我需要在这个背景上添加洛蒂胺化。动画-这是我的网页上的JSON文件。
一些代码打开json文件作为lottie图像:
import streamlit as st
from streamlit_lottie import st_lottie
@st.cache
def load_image_json(path):
""" Load animation and images from json """
with open(path, 'r') as j:
animation = json.loads(j.read())
return animation
back_image = load_image_json('image/background_gradient.json')
我试着这样做,但是行不通的:
page_bg_img = '''
<style>
body {
background-image: {st_lottie(back_image, key='back')};
background-size: cover;
}
</style>
'''
st.markdown(page_bg_img, unsafe_allow_html=True)
我尝试下一个代码,没有错误,但是背景动画没有加载,一切都和以前一样:
def load_lottie_url(url: str) -> dict:
r = requests.get(url)
return json.loads(r.content)
def set_background_lottie(json_data):
json_str = json.dumps(json_data)
tag = f"""<style>.stApp {{background-image: url('data:image/svg+xml,{json_str}'); background-repeat: no-repeat; background-position: center center; background-size: contain; height: 100vh;}}</style>"""
st.write(tag, unsafe_allow_html=True)
json_data = load_lottie_url("https://assets1.lottiefiles.com/packages/lf20_xsrma5om.json")
set_background_lottie(json_data)
1条答案
按热度按时间lskq00tm1#
您是否尝试过示例用法代码片段?