Andereoo/TkinterWeb

load_file on Windows not possible

Cathnan opened this issue · 2 comments

I'm trying to load a local hello world html file using file_url. But execution of the program stops with an error. The error is:

Traceback (most recent call last):
File "p:\VSCode-Server-Workspaces\Python_Projects\tests\GeoPy Test.py", line 28, in
frame.load_file(tempFilePath)
File "C:\Users\heiko\AppData\Local\Programs\Python\Python311\Lib\site-packages\tkinterweb\htmlwidgets.py", line 151, in load_file
file_url = str(file_url).split("/", 1)[1]
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

Do I have to prepare paths on windows in a specific way? The path given to load_file was: path.join(sys.path[0], 'basic_html.HTML')

I'm running windows 11 22H2 Build 22621.1992 and Python 3.11.0

You have to add the local file protocol before the path, i.e. file://. I believe there is a HtmlFrame method that does this automatically though.

(note: I'm not the author, I just had the same problem recently)

EDIT:

After taking a closer look at the source code, it seems that you simply need to put a slash '/' before the drive letter. This is because of line 151 of htmlwidgets.py: file_url = str(file_url).split("/", 1)[1], which appears to specifically target absolute POSIX paths, failing completely on Windows where it either cuts off the drive letter or throws an error like in your case due to the usage of '/' instead of os.sep. For @Andereoo I'd suggest either adding special handling for non-POSIX-compliant systems, or maybe incorporate the above line into the following if statement somehow.

Hi @aponigricon @Cathnan thanks for bringing this to my attention. I've tweaked the logic for loading files; it should work now. Try upgrading TkinterWeb and let me know if it works for you!