TL;DR; How do I get the file path relative to the notebook or package? I have static resources but trouble getting the path to them.
Currently, I have this ugly hack. But is there a better solution for that?
# mynb.ipynb
#| exporti
def get_test_resources_path():
if '__file__' not in globals():
# Case when we run the notebook (mynb.ipynb) itself
p = Path.cwd().parent / 'test_resources'
if not p.exists():
# Case when we run e.g. nbdev_preview from the library root (my-lib/)
p = Path.cwd() / 'test_resources'
else:
# Case when we use the exported script (my-lib/my_lib/mynb.py)
p = Path(__file__).parent.parent / 'test_resources'
assert p.exists(), f"Path {p} does not exist"
return p
This is my project structure:
- my-lib
- nbs
- mynb.ipynb
- my_lib
- mynb.py
- test_resources
- static_file.txt
- MANIFEST.ini
- (other stuff)
To include the static files, i added this to the MANIFEST.ini
recursive-include * test_resources
Any suggestions or solutions? I’m sure I’m not the only one with this problem…
(I’m guessing fastai does something similar with e.g. fastai/nbs/images at master · fastai/fastai · GitHub but i can’t figure out how)