What I’m trying to do:
Load an mp3 file into memory from the “Assets”, and play it.
What I’ve tried and what’s not working:
I added “test.mp3” into the assets and attempted to return a media object it into memory with the following code…
On the client you use _/theme/test.mp3 to get to the test.mp3 file stored at the root of your Assets. No need to involve the server, which apparently can’t access Asset files using the same path anyway.
How do we access the asset files from a server module? I tried setting my button_click function to:
def button_2_click(self, **event_args):
"""This method is called when the button is clicked"""
url = anvil.server.get_app_origin() + "/_/theme/vdespa-medium-whisper-api.mp3"
anvil.server.call('transcribe', url)
However I get a file-not-found error.
Although when I navigate to the URL given as “file not found” in my browser, it is accessible and plays correctly.
How about allowing the user to upload files to the app and have them be accessible by server modules?
And I am still confused why I would be getting a File Not Found error when I am providing the function with an absolute (publicly internet-accessible) file path. The server code must have normal internet access since I am interacting with APIs and the like?
As I understand it, anvil.server.get_app_origin() + "/_/theme/vdespa-medium-whisper-api.mp3" produces a URL (e.g, 'https://www.your_domain_name.com/_/theme/vdespa-medium-whisper-api.mp3').
In contrast, Python’s open() function is intended to work not with the Internet, but with a local file system. It understands Linux file paths, like ./temp/your_file_name_here.xyz.
>py
Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('https://anvil.works/forum/t/how-to-find-arbitrary-asset-path/16802/6')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'https://anvil.works/forum/t/how-to-find-arbitrary-asset-path/16802/6'
>>>
This is why open() fails: it doesn’t understand URLs.
Asset files are definitely stored as part of the App. But not necessarily in a location where a Server-side open() can see them. (They might be database entries, or Git repository entries, for example.) I haven’t tried it, so I don’t know.
If someone knows a Server-side path to the app’s Assets, that would help.
There are examples of this throughout this forum and in the educational materials, if you look.