最近在学习Streamlit模块,自己一不小心发现了一个Streamlit模块中的拓展板块
Lottie动画显示!
在使用这个板块之前,我们需要安装"streamlit-lottie"库
pip install streamlit-lottie
安装完这个库之后,就可以使用'st_lottie'函数展示各种动画效果,而这个函数的使用也有两种方法
● URL方法:用于从互联网加载Lottie动画资源。适用于在线资源,如LottieFiles(需要魔法!)
● 本地文件方法:用于加载已下载到本地的Lottie动画JSON文件。适用于不依赖网络的场景。(下载JSON文件需要进官网下载,Click here!)
方法1:
import streamlit as st
import requests
from streamlit_lottie import st_lottie
def load_lottieurl(url):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
lottie_coding = load_lottieurl("https://assets10.lottiefiles.com/packages/lf20_zzytykf2.json")
lottie_welcome = load_lottieurl("https://assets10.lottiefiles.com/packages/lf20_jcikwtux.json")
lottie_loading = load_lottieurl("https://assets10.lottiefiles.com/packages/lf20_p8bfn5to.json")
st_lottie(lottie_coding, height=300, key="coding")
st_lottie(lottie_welcome, height=300, key="welcome")
st_lottie(lottie_loading, height=300, key="loading")
st.title(":blue[Welcome to my Streamlit App]")
方法2:
def load_lottiefile(filepath: str):
with open(filepath, 'r') as f:
return json.load(f)
lottie_welcome = load_lottiefile('D:\\Deep_Learning\\CSDN_Blog\\welcome.json')
lottie_loading = load_lottiefile("D:\\Deep_Learning\\CSDN_Blog\\loading.json")
lottie_beg = load_lottiefile("D:\\Deep_Learning\\CSDN_Blog\\beg.json")
st_lottie(lottie_welcome, height=300, key="welcome")
st_lottie(lottie_loading, height=300, key="loading")
st_lottie(lottie_beg, height=300, key="beg")
可以看到,方法1和方法2只是函数定义的不同而已,总体而言还是很好理解的。
Tips:上述涉及到的Lottie模块的应用在有魔法的基础上使用更佳。
新手小白的Blog Ending啦,有什么建议欢迎评论区留言~!