How to attach an image in Jupyter Notebook without keeping the file in the same directory?
Asked Answered
S

2

1

I'm trying to use Jupyter Notebook in VS Code (but tested the same thing in Jupyter on browser, and didn't work as well).

If I put the image file in the same directory of the ipynb file, I can drag and drop using the code below, and it works:

![](imagetest.png)

But if I move the image file to other directory, or delete it, the image doesn't appear anymore.

I tried to use:

![imagetest.png](attachment:imagetest.png)

as I saw in others examples (which was working fine), but when I try the same, it doesn't work.

I want to attach the image in ipynb file and make it independent of the source image file, as I can do in a MS Word file, for example.

Southsoutheast answered 4/9, 2022 at 1:47 Comment(0)
H
0

First try copying the image and pasting it, run the cell and see if it works. If it works, it will work every time. After pasting the picture the code looks like this:-

![image.png](attachment:image.png)

Next, Run the cell and it will be inserted.

Pic of pasting picture:- https://i.sstatic.net/XI4Il.png

Pic of picture inserted:- https://i.sstatic.net/DFA4O.png

Picture added

Pasting picture

Habitable answered 4/9, 2022 at 1:51 Comment(3)
Tks, it works in Jupyter Notebook (Anaconda3) using the browser. But when I try the same in Jupyter inside VScode, doesn't work. It just paste the name like "image.png", not "![image.png](attachment:image.png)" as expected. In VScode I tried to type "![image.png](attachment:image.png)" but nothing is shown in the notebook. Maybe is a VSCode issue I'm facing? Some setup?Southsoutheast
@Southsoutheast this option is not available in vscode's Jupiter. you need to save the file. Next use a code like this: - [title](fig1.png) in markdown. For your reference: - #58401470Habitable
Tks again. Just to note, in the link you showed here says it`s possible now do this, just need to do this code.visualstudio.com/updates/v1_71#_jupyterSouthsoutheast
C
0

You can use relative path syntax.

If you move the image into the parent directory you can prefix it with .. which means "up one directory".

![imagetest.png](../imagetest.png)

If you move it to a subdirectory, you should be able to prefix it with the path to that subdirectory.

![imagetest.png](sub/directory/path/imagetest.png)
Cordite answered 4/9, 2022 at 1:51 Comment(0)
H
0

First try copying the image and pasting it, run the cell and see if it works. If it works, it will work every time. After pasting the picture the code looks like this:-

![image.png](attachment:image.png)

Next, Run the cell and it will be inserted.

Pic of pasting picture:- https://i.sstatic.net/XI4Il.png

Pic of picture inserted:- https://i.sstatic.net/DFA4O.png

Picture added

Pasting picture

Habitable answered 4/9, 2022 at 1:51 Comment(3)
Tks, it works in Jupyter Notebook (Anaconda3) using the browser. But when I try the same in Jupyter inside VScode, doesn't work. It just paste the name like "image.png", not "![image.png](attachment:image.png)" as expected. In VScode I tried to type "![image.png](attachment:image.png)" but nothing is shown in the notebook. Maybe is a VSCode issue I'm facing? Some setup?Southsoutheast
@Southsoutheast this option is not available in vscode's Jupiter. you need to save the file. Next use a code like this: - [title](fig1.png) in markdown. For your reference: - #58401470Habitable
Tks again. Just to note, in the link you showed here says it`s possible now do this, just need to do this code.visualstudio.com/updates/v1_71#_jupyterSouthsoutheast

© 2022 - 2024 — McMap. All rights reserved.