Accessing .txt asset from client module

Hello,

I’ve uploaded a .txt file into my assets and would like to read from it in a client module.

I’ve been unable to find the relative path that would get me to the text file. I’ve tried things like:

with open("../theme/my_file.txt", "r") as my_file:

But I always get a File not found error.

Any help here would be much appreciated!

If that is going to work at all (which I’m not sure about) then the path you want is

“_/theme/my_file.txt”

I really wasn’t thinking when I posted that. Ignore!

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

Using a Data Table with a Media column did the trick. Thanks for your help

1 Like

What if you only want to read it from a server module and NOT client code. In particular: reading a csv file from a background task in order to populate a database table?

1 Like

Hi Ben,

The code snippet you quoted will work in the server just fine!

1 Like

I’m storing some files in a Data Table with a Media column as suggested. My problem is I have a bunch of files and they change once in a while (through local editing).

Is there a way to “bulk upload” them to the Data Table with python code via Uplink?

Your Uplink code can certainly access Data Tables. It’s explicitly supported. See Connecting to Data Tables.

2 Likes

Thanks. What I meant was how do I “send over” the files to the database. The answer is with BlobMedia objects like so (for future reference):

for file_name in os.listdir(path):
    with open(path + file_name, 'rb') as file:
        file_contents = file.read()
        media = anvil.BlobMedia(content_type="text/plain", content=file_contents, name=file_name)
        table.add_row(name=file_name.split('.')[0], file=media)

(I was confused at first because I thought Media objects can only be certain “Media” files like images, videos, etc. which is of course not the case, can be any binary string file content)

1 Like

Glad you found it! That’s right; any bytes object will do. It doesn’t even have to come from a file, per se.