Returning an image from server code

Hi all,

I have a function in my library (running on a Digital Ocean droplet connected to Anvil) that creates and returns a matplotlib figure. Is it possible to send back this figure as an image?

The code on the digital ocean droplet is

@anvil.server.callable
def do_cpt_to_img(file) -> Dict:
    s_utf8 = file.get_bytes().decode("utf-8")
    cpt = Cpt.from_string(s_utf8)    
    return cpt.plot() # will return a matplotlib figure

And I would like to return this to be used as an image on a page like;

self.image.source = anvil.server.call('do_cpt_to_img', cpt_file)

Thanks in advance,
Rob

1 Like

Yes, that’s perfectly doable. The docs describing it are at: Anvil Docs | Files, Media and Binary Data

2 Likes

Even more specifically, we have a how-to guide on using Matplotlib (and other libraries) with Anvil!

2 Likes