css 如何在背景流光应用程序中添加洛蒂动画?

r3i60tvu  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(109)

我有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)
lskq00tm

lskq00tm1#

您是否尝试过示例用法代码片段?

import time
import requests

import streamlit as st
from streamlit_lottie import st_lottie
from streamlit_lottie import st_lottie_spinner

def load_lottieurl(url: str):
    r = requests.get(url)
    if r.status_code != 200:
        return None
    return r.json()

lottie_url_hello = "https://assets5.lottiefiles.com/packages/lf20_V9t630.json"
lottie_url_download = "https://assets4.lottiefiles.com/private_files/lf30_t26law.json"
lottie_hello = load_lottieurl(lottie_url_hello)
lottie_download = load_lottieurl(lottie_url_download)

st_lottie(lottie_hello, key="hello")

if st.button("Download"):
    with st_lottie_spinner(lottie_download, key="download"):
        time.sleep(5)
    st.balloons()

相关问题