1

I'm trying to develop a visualization package based on CEFpython technology, so the function has to access an html file. So the structure of the package is basically:

viz_tool

--> init.py

--> main.py

--> index.html

and in the main.py file, I defined a function:

def view():
    index_filepath = os.path.realpath("index.html")
    print(index_filepath)
    browser = cef.CreateBrowserSync(url='file://' + index_filepath,
                                window_title="viz tool")
    ....

The goal is, after installing this package, I can do:

from viz_tool import view
view()

but of course currently the "index_filepath" would be my current working directory, so it won't be able to find the "index.html" file.

How should I change the code so I could access the "index.html" file, which presumably would be in the directory where the package is installed?

Thank you very much! and please let me know if I need to clarify anything

lxiangyun93
  • 77
  • 1
  • 7

1 Answers1

3

Look at the documentation of pkg_resources.
It has a method called resource_filename that you can use to get absolute path to any internal module.

from pkg_resources import resource_filename
from foo.bar import bazz # say bazz has template folder

pth_to_template = resource_filename(bazz.__name__, "template")

This will return the absolute path to <path to foo>/foo/bar/bazz/template.

I would highly recommend not putting html file in root folder and also avoid using hardcoded paths in any form, including doing os.path.join recursively to traverse multiple levels. It makes moving files in future a mess.

Edit: For accessing resources from the same module by relative path, you can do

pth = resource_filename(__name__, "templates/something.html")

Edit 2:. You will also have to add all the non-python files in package_data as here in order for setup.py to add them to the distribution package.

dumbPy
  • 1,379
  • 1
  • 6
  • 19
  • it seems in this way I need to change the code when loading the view function. is there a way to change the function itself to get the same result? also, in this case, what would you recommend if I don't put the html in the package? where else should I put it? – lxiangyun93 May 15 '20 at 20:12
  • just add a directory names templates. That's what frameworks like Django do. – dumbPy May 15 '20 at 20:17
  • @lxiangyun93 I guess you should have a folder for this kind of static file (just not directly the file in the root folder of the package), see https://stackoverflow.com/questions/11848030/how-include-static-files-to-setuptools-python-package or https://stackoverflow.com/questions/53233670/accessing-static-files-included-in-a-python-module if needed to include these files when distributing the package. – mgc May 15 '20 at 20:18
  • Yes, that too... I guess I misunderstood your question as to how to access the data files, based on the other answer that suggested using `os.path.join` and `__file__`. Yes, for any non python file, you will have ti add it as a data file too – dumbPy May 15 '20 at 20:28
  • @dumbPy Thanks a lot! it worked! Would you mind changing the main answer to the one you suggested in the comment, so I could accept the answer? – lxiangyun93 May 15 '20 at 21:21
  • @lxiangyun93 Added to answer as requested. – dumbPy May 15 '20 at 21:36