Animated pyplot?

I’m trying to save an aminated gif to a data table, and then display the result in a form. It seems my program successfully generates the sine wave and saves it as a gif, but how to get it into the table, and then display it in a form?

When I try import anvil.image, the server says there is no such module …

My code is exactly as the first part of a YouTube video, Animating Plots In Python Using MatplotLib [Python Tutorial] - YouTube at timecode 5:11, and works at it should locally in PyCharm. Now if I did know for sure that the gif was saved, and where, that would probably help, but I’m stuck.

I uploaded the gif from my disk and put it into a table, using code to display it in a form, from that table, and that worked, but I want to be able to generate the gif on server, then put it into the table and after that, display the generated gif in the form. Hope You have time to look into this, it would be much appriciated.

In general, if you are programmatically generating a picture (gif/whatever) then you will be creating a Media Object which can be stored in data tables.

This (very old) post of mine gives an example of creating media in code, and a search of the docs will explain how to store it :

You do also have access to your own private “/tmp” but this is ephemeral and files stored there may not survive outside of the server call that put them there, so be careful if using this.

This is worth a read, too :

1 Like

Okay, I see. I managed to save something to the table, but seems what I got is not what I wanted.

Thanks very much man, but I managed to do it in another way, at last, the gif has been put into the table, using anvil.media and anvil.media.from_file(“sinwave.gif”, “image/gif”). However, I used the code seen here: Saving Animations with PillowWriter in Python - PythonAlgos

I first ran the server code, saving the animated gif to table, then made a call to server for the anvil function, and then put the media content of the row into an image component on the form.

But I updated the table using your code, was not aware of thet way of doing it, I’m a total noob when it comes to anvil.

anvil_sine_wave

But server really lags on this one, takes about 10 secs.

So far, this is part of a larger project, so I cannot share the code yet, but will copy to new project, so You can see how it finally was accomplished, if you so wish.

Here it is:
https://anvil.works/build#clone:L7RFSRF337ZQAT4C=VDI6Y3N76XOGUG2FY77HS2SV