[App Server] Issue displaying image from local server (Python 2 vs 3)

Hi everyone, first I’d like to say a massive thanks to everyone at Anvil for making this tool available, and to everyone on these forums for your answers to other questions, as they have been a massive help over the last few months. While I’m not completely new to coding I’m by no means fantastic at it so this may be a simple fix, but it has me at a loss!

I’ve encountered a bit of a weird issue. I’m running my app using the server app on a Raspberry Pi (as I only need it accessible off the local network). I previously had it installed and working on a system using Python 2.7 as the default, however I wanted to change it over to Python 3 as Python 2 is now unsupported. Having installed all the required libraries again (which provided a valuable lesson in the need for some kind of documentation as you go!) I’ve got myself to a point where the app loads up. The basic point of this part of the app is to take two photos with webcams and then display the images on the screen so the user can see them. While the image content changes over time they always have the same file names.

Now here comes the bit that has me stumped. Previously I was able to save and load the images from my _/theme/assets/ folder to display in the app using the simple anvil.media.from_file command. Since I’ve moved across to Python 3 this is no longer working with some .jpg files. Any time the server module tries to run the the command for some image files it returns the following error in the command line:
“EOFError while reading worker stdout. This should not have happened.
Server code exited unexpectedly: dad065712e (IDs client-CYWZieXGMVgQJ1fpv5HWBw==)”

I’ve included the relevant snippets of both the client and server side code below to show how the code is currently written.

From my basic checking it seems to be an issue around ‘larger’ files. In this case both leftPreview.jpg and rightPreview.jpg are just over 1MB, while recordSymbol.jpg is only 11kB. When run with recordSymbol.jpg as the file to be returned then type(text) returns <class ‘anvil.BlobMedia’> as you would expect.

Fortunately while trying to figure this out I had a spare microSD hanging around, so I installed a clean copy of Raspberry Pi OS on that and set everything up with Python 2 still as the default and it runs as expected (displaying the larger images from file, and from fresh camera captures) so I’m 95% sure that my issue lies with some difference between Python 2 & 3 rather than some difference in the versions of Anvil between my old setup and new.

I’ve tried setting the image source attribute to the file’s locations, however this just displays an empty space. I’ve also stopped the cameras from taking images and just left the two preview images as fixed assets and the error message still occurs.

Any help or suggestions would be gratefully received!

Client Code

print("[INFO]... Capturing Left Preview")
test = anvil.server.call('previewImgCap', 0)
print("[INFO]... Test object type:")
print(type(test))
self.cam_left_preview.source = test
print("[INFO]... Capturing Right Preview")
anvil.server.call('previewImgCap', 1)
self.cam_right_preview.source = anvil.server.call('previewImgCap', 1)

Server Code

@anvil.server.callable
def previewImgCap(camNum=0):
    themeLoc = "/home/pi/LaxCam/theme/assets/"
    if camNum is 0:
        # Return preview image as media object to client code
        print("[INFO]... Left Preview Taken.")
        leftFile = anvil.media.from_file(themeLoc + 'leftPreview.jpg', 'image/jpeg')
        #leftFile = anvil.media.from_file(themeLoc + 'recordSymbol.jpg', 'image/jpeg')
        return leftFile
    else:
        preview_image_code.rightCap()
        return anvil.media.from_file(themeLoc + 'rightPreview.jpg', 'image/jpeg')
        #return anvil.media.from_file(themeLoc + 'recordSymbol.jpg', 'image/jpeg')

Hi @JM90 - welcome to the forum!

Can you post an app that others could launch with the App Server and demonstrate the problem? If a strange problem only occurs on one person’s raspberry pi it’s very difficult to track down (or to distinguish from a weird issue with that machine!). The easiest way is probably to cut down your app until it has just enough code to replicate the issue, then put it on GitHub - as a fresh repository if you don’t want the history included, if course.

1 Like

Yeah I’ll give that a go, but it may take me a little while as I have some other ‘real world’ tasks to do first :sweat_smile:. I’ll post the link in here when I do/ if I figure out what the problem is along the way!

So I finally managed to get round to cutting the app down, and didn’t get a repeat of the problem. After some further thought and tinkering it seems to have been an issue with my hardware, as you hinted.

In this particular case the root of the problem seems to have been the microSD card, a 128GB Samsung Evo Plus. When I swapped it out and reinstalled everything with a 32GB SanDisk Ultra all the issues disappeared. I’m still unsure whether this is a brand issue or an issue with using a microSD card over 64GB, but I guess that’s where my Saturday afternoon will go!

Thanks for your suggestion as I don’t think I would have thought to check the card if I hadn’t gone down this path and it’s associated rabbit holes.