Does anyone know what a good way is to implement absolute paths in a jupyter notebook for markdown image files on a Jupyterhub?
After some hacking around, I found that this works OK but is my notebooks will not be user independent since the path on the jupyterhub contains the username:
Absolute file paths would be best but do not work (which I guess makes sense…)
My application is to make an autogenerated image gallery. One option I could consider is maybe something with the Image objects in the cell output:
I guess this will probably be my next step if there is no other way
I specifically want to generate a markdown cell that contains an image gallery of all images in folders mounted in my local jupyterhub. As all users are mapped to jovyan, the files actually all have the same local path (which also makes it easy for us to share data analysis notebooks seamlessly)
But this all breaks for images since they are server path specifications, not local file path.
I think my only way out is the symlink option, I’ll give it a try.
Note that there are two paths involved: the filesystem path and the path used by your browser. Basically anything that you want to include in markdown will be resolved by your browser querying that path (as a part of the URL).
This means that the paths being the same in the container isn’t enough. Also the browsers of different users have to be able to access the files under the same path.
Hi , I faced a similar problemin order to succesfully deploy my online Jupyter book , because I need to retrieve data locally from my computer and it needed to be a relative path.
I dont know if it is exactly the same issue as @gsteele13 is facing , but I post my solution just in case it serves as any help:
import os
script_dir = os.path.dirname('__file__') #<-- absolute dir the script is in
rel_path = "datasets/my-data.xlsx" #relative path in reference to where is the script
abs_file_path = os.path.join(script_dir, rel_path)
os.chdir('mini_book/docs/') # like `cd`
data_to_analyze=pd.read_excel(abs_file_path)
My solution in the end was to make symlinks in the local folder so that the files appear as a relative path. It turned out the images were mega-big anyway, so I had to make thumbnails for performance issues anyway, which I then also put in am auto-generated local path.
The final code you can find here if you are interested: