Accessing .txt asset from client module

Hi there, and welcome to the forum!

You can get the contents of an asset as a Python string by fetching it as URLMedia. Here’s an example:

url = anvil.server.get_app_origin() + "/_/theme/my_file.txt"
file_contents = URLMedia(url).get_bytes()

But if you want to store files for use in your app, I’d recommend using a Data Table with a Media column in it. Let’s say I have this table in my app:

image

Now I can fetch the contents of that file like this:

contents = app_tables.files.get(name='my_file')['data'].get_bytes()

Note that I set the permissions on that table so that client code can read it. Data Tables can hold much more data than theme assets, so this is the solution I would recommend.

Hope that helps!

4 Likes