flet
flet copied to clipboard
Add Image from File and Memory ( from bytes )
-
Image for file f = open("path) page.add(Image(src=f)
-
Add also binary image example base64 image
Do I understand correctly that you would like the convenience of not having to do the base64 encoding yourself if Image is given a file-like object?
@FeodorFitsner, I can take this is you are happy with the approach that src
accepts either a str
or a file-like object?
File-like object sounds like a great idea. The question is should we save it to "assets" under temp name and use that URL or convert to base64 and push through WebSocket? What would be more effective?
My quick take is that converting it to base64 means that the file only comes from disk and then goes over the WebSocket ("2 transfers"), but with the asset approach we might take it from disk, save it to disk, and send it over the network ("3 transfers"). Of course the first also has the base64 encoding, but I have grown up thinking that that kind of CPU operation is always faster than going over the network. 🤷🏻♂️
Well, maybe that is splitting hairs a bit too much, and my main argument would be just against saving temp files to assets, as that seems "not neat".
My main concern here is the size of WebSocket message. Flet icons browser app takes that to "extrem hights" by allowing messages for up to 8 MB :), but my observation is that some messages aroung 500 Kb are quite "acceptable". So, it will work for images of ~500 KB size in base64-encoded form.
Ok, so how about: src accepts str or file-like, and saves the file-like to assets, src_base64 accepts bytes or file-like to be base64 encoded, with warning in the docs about the file size (if not already there). Or is this getting too fancy?
Alternative could be e.g. src_file, which could decide which method to use based on the file size (or just use assets for everything).
I say that how to display bit image