Absolute paths to markdown images in a jupyterhub

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:

Screenshot 2020-06-01 at 16.57.41

Absolute file paths would be best but do not work (which I guess makes sense…)

Screenshot 2020-06-01 at 17.03.07

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

Thanks!
Gary

Hmm. This does work:

Screenshot 2020-06-01 at 17.08.52

but is not what I want since I want them eventually in a table.

And then this suffers from same problems:

Screenshot 2020-06-01 at 17.08.57

Hmm.

But that’s the whole point of absolute paths: they aren’t portable. Why do you specifically want absolute and not relative paths?

some more follow up:

but not what i need…

not looking promising…

https://stackoverflow.com/questions/41604263/how-to-display-local-image-in-markdown/41912122

probably for deployment on my jupyterhub, a nasty hack like a unix symlink is going to be the best solution…should work though.

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)

Best,

Leila

Thanks Leila

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:

1 Like

(this is an evolution and renaming of an earlier repo called “data-summary-generator”)