Skip to content

Instantly share code, notes, and snippets.

@asehmi
Created April 19, 2022 15:21
Show Gist options
  • Save asehmi/31917f18890abb6fc1f70a874737c602 to your computer and use it in GitHub Desktop.
Save asehmi/31917f18890abb6fc1f70a874737c602 to your computer and use it in GitHub Desktop.
Streamlit echarts animation demo
import json
import random
import time
import streamlit as st
# pip install streamlit-echarts
from streamlit_echarts import st_echarts
st.title("I ❤️ ECharts")
c_type = st.radio('Chart type', ['bar', 'line', 'k', 'scatter'])
placeholder = st.empty()
if "iteration" not in st.session_state:
st.session_state["iteration"] = 0
rnd = random.randint
if c_type in ['bar', 'line']:
data = [rnd(0, 100) for _ in range(7)]
elif c_type == 'k':
data = [[rnd(0, 100),rnd(0, 100),rnd(0, 100),rnd(0, 100)] for _ in range(7)]
elif c_type == 'scatter':
points = st.slider('Number of data points', min_value=10, max_value=1000, value=100, step=10)
data = [[rnd(0, 100),rnd(0, 100)] for _ in range(points)]
option = {
"xAxis": {
"type": "category",
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
},
"yAxis": {"type": "value"},
"series": [
{
"data": data,
"type": c_type,
"showBackground": True,
"backgroundStyle": {"color": "rgba(180, 180, 180, 0.2)"},
}
],
}
if c_type == 'scatter':
option["xAxis"] = {}
option["yAxis"] = {}
st.markdown("---")
forever = st.checkbox('Loop forever', False)
while st.session_state["iteration"] < 10:
st.markdown(f"Iteration number {st.session_state['iteration']}")
with placeholder:
st_echarts(option, height="500px", key="chart")
time.sleep(1)
st.session_state["iteration"] += 1
if st.session_state["iteration"] < 10:
st.experimental_rerun()
if forever:
st.session_state["iteration"] = 0
st.experimental_rerun()
@asehmi
Copy link
Author

asehmi commented Apr 19, 2022

echarts-animation-demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment