0

I tried to plot a gif on my streamlit site, but it doesn’t generate anything. It doesn’t give any error, it just keeps the environment empty as if nothing had been run in the code.

Below I left the piece of my code responsible by the plot of the gif.

graph, = plt.plot([], [], color="gold", markersize=3, label='Tempo: 0 s')
        L = plt.legend(loc=1)

plt.close()  # Não mostra a imagem de fundo

def animate(i):
     lab = 'Tempo: ' + str(round(dt*i * (rs_sun / 2.0) * 3e-5 , -int(math.floor(math.log10(abs(dt*(rs_sun / 2.0)*3e-5)))))) + ' s'
     graph.set_data(x[:i], y[:i])
     L.get_texts()[0].set_text(lab)  # Atualiza a legenda a cada frame
     return graph,

skipframes = int(len(x)/200)
if skipframes == 0:
     skipframes = 1

ani1 = animation.FuncAnimation(fig, animate, frames=range(0,len(x),skipframes), interval=30, blit = True, repeat = False)
        
plt.show()
vvvvv
  • 25,404
  • 19
  • 49
  • 81
User8563
  • 123
  • 1
  • 2
  • 13

1 Answers1

0

Streamlit only allows .jpg and .png images using st.image(img), but there's a way you can post GIF's.

That's how I have done on my app:

file = open(r"path", 'rb')
contents = file.read()
data_url = base64.b64encode(contents).decode('utf-8-sig')
file.close()
st.markdown(f'<img src="data:image/gif;base64,{data_url}>',unsafe_allow_html = True)
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Rafael Colombo
  • 339
  • 1
  • 2
  • 8
  • Hi Rafael. Thanks for the answer. I had already seen this way of importing GIFs, but in my case I made a program that calculates an orbit of a body around a star and the result is a GIF of that orbit. I even managed to generate it, but in streamlit, the result takes almost 1 minute to plot (in Pycharm it takes about 15 seconds). Is there a way to decrease this plot time on streamlit? Trying to plot the way you exemplified or changing something in the code? – User8563 Jul 10 '22 at 19:46