As a result, i get some kind of html player, where i can click on Play button and start my animation drawing, however i want that animation starts immediately when player appears. In other words, can i somehow set autoplay mode to the player?
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
import streamlit as st
from itertools import cycle
## Generate some data
randomdata = np.random.random(size=(5,3,3))
harvest_cycle = cycle([i for i in randomdata])
harvest = next(harvest_cycle)
## Initialize figure
fig,ax = plt.subplots(figsize=[3,3])
ax.set_aspect('equal')
ax.grid(which = 'minor',color='w', linestyle='-', linewidth=2)
heatmap = ax.imshow(harvest)
## Animate
def update(frame_number):
## Clears the data passed to the axes
## This is important so the final figure does not store
## every single frame and slows everything down
ax.cla()
ax.imshow(next(harvest_cycle))
ax.set_title(f"Frame: {frame_number}",fontsize=24)
animation = FuncAnimation(fig,update,frames=10)
## Export jshmtl representation
animjs = animation.to_jshtml()
### Inject JS for autoplay
import re
## JS line to find the play button and click on it
click_on_play = """document.querySelector('.anim-buttons button[title="Play"]').click();"""
## Search for the creation of the animation within the jshtml file created by matplotlib
pattern = re.compile(r"(setTimeout.*?;)(.*?})", re.MULTILINE | re.DOTALL)
## Insert the JS line right below that
new_animjs = pattern.sub(rf"\1 \n {click_on_play} \2", animjs)
st.markdown("## :control_knobs: Using `jshtml` from matplotlib")
cols = st.columns(2)
with cols[0]:
"### π No modification"
st.components.v1.html(animjs,height=600)
with cols[1]:
"### βΆοΈ Autoplay"
st.components.v1.html(new_animjs,height=600)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking βAccept allβ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.