Hitheshrai commited on
Commit
96c29e1
·
verified ·
1 Parent(s): 680207c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
2
  import matplotlib.pyplot as plt
3
  import numpy as np
4
  import matplotlib.animation as animation
5
- from io import BytesIO
6
  import base64
7
 
8
  # Set up page configuration
@@ -55,11 +55,12 @@ def update(frame):
55
  electron.set_data([x], [y]) # Wrap x and y in lists to avoid RuntimeError
56
  return electrons
57
 
58
- # Generate animation
59
- ani = animation.FuncAnimation(fig, update, frames=range(200), interval=50, blit=True)
 
 
 
 
60
 
61
- # Save animation as HTML and embed in Streamlit
62
- html_anim = BytesIO()
63
- ani.save(html_anim, writer="html", fps=20)
64
- html_data = base64.b64encode(html_anim.getvalue()).decode("utf-8")
65
- st.components.v1.html(f"<div align='center'><img src='data:image/gif;base64,{html_data}'></div>", height=500)
 
2
  import matplotlib.pyplot as plt
3
  import numpy as np
4
  import matplotlib.animation as animation
5
+ import tempfile
6
  import base64
7
 
8
  # Set up page configuration
 
55
  electron.set_data([x], [y]) # Wrap x and y in lists to avoid RuntimeError
56
  return electrons
57
 
58
+ # Generate animation and save as temporary HTML file
59
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
60
+ ani = animation.FuncAnimation(fig, update, frames=range(200), interval=50, blit=True)
61
+ ani.save(tmpfile.name, writer="html", fps=20)
62
+ tmpfile.seek(0)
63
+ html_data = tmpfile.read().decode("utf-8")
64
 
65
+ # Display animation in Streamlit
66
+ st.components.v1.html(html_data, height=500)