I have uploaded a .png file via assets to my application. How can I access the .png file from server code? I am using python pillow library and need to access this .png as a file. The goal is to combine this .png file with some text and generate a new .png file which can then be available through a URL.
The docs cover that: Anvil Docs | Themes and Styling
I will note that you can use a relative URL when accessing the files from client or server code, so you can leave off the https://myapp.anvil.app
portion.
Thanks … so I guess I need to use python requests to get the file via url . However when I use requests.get('_/theme/Human.png')
I get an error MissingSchema: Invalid URL '_/theme/Human.png': No scheme supplied. Perhaps you meant http://_/theme/Human.png?
However using http:// or https:// does not help either. I tried to also use request.get(anvil.server.app_origin() + ‘_/theme/Human.png’) but that does not work either.
Using Python requests would incur more network traffic, and would require the full URL (with the domain name). Try just using the relative URL where you need a file path.
For example, on the client you could use this as the image source: /'_/theme/Human.png'
On the server, if you’re using pillow you probably have a function that needs a file path. Try the relative URL.
If that doesn’t work, you’ll want to post the code that you’re trying to write (e.g. the pillow routines that you’re trying to use the asset in), so that folks can see more context and provide better help.
that’s the first thing I tried logo = Image.open('_/theme/Human.png')
- error received was FileNotFoundError: [Errno 2] No such file or directory: '_/theme/Human.png'
Here is my sample app code
Looks like you’ll need to go by way of a media object, then. You can create a URLMedia object from the URL, and from there either get the bytes or create a temporary file. Both are covered in the docs section on media objects: Anvil Docs | Files, Media Objects and Binary Data
There’s also a blog post in the forum about using Pillow and Anvil. Looking at how that project did it might help with what you’re trying to do. Just do a forum search on pillow and look for the cross-stitch blog post.
Edit: you might also find the new Data Files service helpful. Anvil Docs | Data Files