How to load local lottie json file with st_lottie component?

@andFanilo
Hi, I tried your code, find if I use the web url, lottie can be displayed normally.

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

lottie_url = "https://assets7.lottiefiles.com/packages/lf20_i9mxcD.json"
lottie_json = load_lottieurl(lottie_url)
st_lottie(lottie_json)

when I download the lottie json file and load it locally, it can not be played

from streamlit_lottie import st_lottie
st_lottie("tree-in-the-wind.json")

can we achieve json file load locally?
Thank you.

Hey @BeyondMyself,

The method takes a Python Dict as input, you’ll need to read/parse the JSON file first, something like:

from streamlit_lottie import st_lottie
with open("tree-in-the-wind.json", "r") as f:
    data = json.load(f)
st_lottie(data)

You can check an example code here

Have a nice day :balloon:
Fanilo

1 Like

thanks. it works, if report unicode error, we need add errors=‘ignore’ in code:

from streamlit_lottie import st_lottie
import json
with open("tree-in-the-wind.json", "r",errors='ignore') as f:
    data = json.load(f)
st_lottie(data)

1 Like