Reading HTML file in the client side

What I’m trying to do:
I have an HTML file that I read from the server side, and it takes it too much time to load.
I am trying to bypass it with client side with no success. Here is the server side that works:

@anvil.server.callable
def get_HTML():

 html_string_path = 'some_file.html'
 with open(html_string_path, 'r') as f:
    html_string = f.read()
 return html_string

#Client side:
html_name = "get_HTML"
self.html_mechanic_for_gui(html_name)

def html_mechanic_for_gui(self,html_name):
    self.html_panel.remove_from_parent()
    srcdoc = anvil.server.call(html_name)
    html = "<iframe style='position:absolute;width:85%;height:100%;left:15%;top:0;border:none' anvil-name='iframe' ></iframe>"
    self.html_panel = HtmlTemplate()
    self.html_panel.html = html
    self.html_panel.dom_nodes["iframe"].srcdoc = srcdoc
    self.add_component(self.html_panel)

What I’ve tried and what’s not working:
I am trying to bypass the server side by simply loading the file to asset and then try to read it directly in the client side as follows:

html_name = anvil.http.request("/_/theme/some_file.html" , method='GET')


I get an error that the app can’t find the file, also I am not sure that this is the same format as the server side trick.
Any help would be great.

Meredydd’s solution for the .txt file also works for html files in the assets section.

2 Likes

It seems similar but I need the HTML to be in a string format, from what I see in the example and in the docs there is no way to do it, other than using the server side…
Is that correct?

Just string the get_bytes content if necessary. html files are really just fancy text files anyways and just contain text content. When verifying the fix works for html too I printed the output to check and it was all proper string in the console.

I tried:

file_contents = str(URLMedia(url).get_bytes())

That didn’t work, what am I missing?

A byte string must be decoded into a string, something like file_string = file_contents.decode('utf-8')

1 Like

Thanks!!!
That solved it all!
Now I wonder how can I put a “Please wait” banner when the app is loading for the first time?
It takes few more seconds to the app to load due to assets loading I assume.

You could trigger a notification and set its timeout to 4, or something similar.