spatialaudio / nbsphinx

:ledger: Sphinx source parser for Jupyter notebooks
https://nbsphinx.readthedocs.io/
MIT License
451 stars 130 forks source link

Attachments with same name displayed incorrectly #500

Open danlkv opened 3 years ago

danlkv commented 3 years ago

If I paste a screenshot in a cell, it appears as a attachment:image.png - the default name for image.

It looks like nbsphinx uses global naming scope for images, because all my images pasted in the notebook render to the same picture. I can't seem to be able to rename attachment from "attachment toolbar" in notebook.

I guess this is what this line means: https://github.com/spatialaudio/nbsphinx/pull/114/files#diff-4b6afd0fa4c9aae80ae389f523f5a1a5R542

danlkv commented 3 years ago

I just figured out that the global name scope is, in fact, in the .rst document. The problem is band-aided by using different captions for images, having [image1.png](attachment:image.png) and [image.png](attachment:image.png) works fine.

mgeier commented 3 years ago

Thanks for the report!

Can you please provide a minimal example for me to reproduce the problem?

Your description of the problems sounds a bit like #162, can you try it with a Pandoc version >= 2.10?

danlkv commented 3 years ago

I tried the minimum example from #162 and can confirm that both images are 'B'. My pandoc seems to be up-to-date.

» pandoc -v
pandoc 2.10.1
Compiled with pandoc-types 1.21, texmath 0.12.0.2, skylighting 0.8.5
Default user data directory: /home/dali/.local/share/pandoc or /home/dali/.pandoc
...
mgeier commented 3 years ago

@danlkv can you please try my little Pandoc test from https://github.com/jgm/pandoc/issues/6194 with your latest Pandoc version?

danlkv commented 3 years ago

Looks well:

» pandoc -f markdown -t rst
image1: ![foo](x.png)

image2: ![foo](y.png)
^D
image1: |foo|

image2: |image1|

.. |foo| image:: x.png
.. |image1| image:: y.png
mgeier commented 3 years ago

Hmmm, that's strange. Your Pandoc version behaves correctly, yet the HTML pages generated with nbsphinx are wrong ...

Are there any warnings when running Sphinx?

danlkv commented 3 years ago

Here's the output:

» make html
Running Sphinx v3.2.1
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: [new config] 2 added, 0 changed, 0 removed
reading sources... [100%] notebook                                                                                                   
/home/dali/git-bulit/nbsphinx_image_error/notebook.ipynb:37: WARNING: Duplicate substitution definition name: "image1".
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] notebook                                                                                                    
waiting for workers...
generating indices...  genindexdone
copying notebooks ... [100%] notebook.ipynb                                                                                          
writing additional pages...  searchdone
copying images... [100%] B.png                                                                                                       
copying static files... ... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 1 warning.

The HTML pages are in _build/html.

I also did a depth-5 search in my root dir for pandoc, in case there is a duplicate bin somewhere - found the only one in /usr/bin...

mgeier commented 3 years ago

Can you please provide a minimal example for me to reproduce the problem?

Are the images in different Markdown cells (but in the same notebook)?

danlkv commented 3 years ago

I use the very same repo from #162, here are precisely the commands I execute:

» git clone git@github.com:martintb/nbsphinx_image_error.git
Cloning into 'nbsphinx_image_error'...
remote: Enumerating objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Receiving objects: 100% (13/13), 15.52 KiB | 611.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.
dali at archlinux in ~/git-bulit
» cd nbsphinx_image_error/
dali at archlinux in ~/g/nbsphinx_image_error (master|✔)
» mkdir _static
dali at archlinux in ~/g/nbsphinx_image_error (master|✔)
» make html
Running Sphinx v3.2.1
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: [new config] 2 added, 0 changed, 0 removed
reading sources... [100%] notebook                                                                                                                                                                                                                             
/home/dali/git-bulit/nbsphinx_image_error/notebook.ipynb:37: WARNING: Duplicate substitution definition name: "image1".
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] notebook                                                                                                                                                                                                                              
waiting for workers...
generating indices...  genindexdone
copying notebooks ... [100%] notebook.ipynb                                                                                                                                                                                                                    
writing additional pages...  searchdone
copying images... [100%] B.png                                                                                                                                                                                                                                 
copying static files... ... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 1 warning.

The HTML pages are in _build/html.
dali at archlinux in ~/g/nbsphinx_image_error (master|…99)
» cat _build/html/notebook.html | grep image
  <title>Image Test &mdash; nbsphinx_image_error 1.0.0 documentation</title>
            <a href="index.html" class="icon icon-home" alt="Documentation Home"> nbsphinx_image_error
          <a href="index.html">nbsphinx_image_error</a>
<p><img alt="image1" src="_images/B.png" /></p>
<p>The images above and below this header should be different.</p>
<p><img alt="image1" src="_images/B.png" /></p>

As you see, there are two images in the html file, both B.png and I see them if I open the page in browser as well.

I also checked what file python reads

» python -vc 'import nbsphinx' 2>&1 |grep nbsphinx
# code object from /usr/lib/python3.8/site-packages/nbsphinx.py
import 'nbsphinx' # <_frozen_importlib_external.SourceFileLoader object at 0x7f9855db7be0>
# cleanup[2] removing nbsphinx
# destroy nbsphinx

and just in case:

» cat /usr/lib/python3.8/site-packages/nbsphinx.py |sha256sum 
5556ac04a489450a8dfb3203e1f92aceda0b5d26c0e152c4e7351bda33448242  -
mgeier commented 3 years ago

I use the very same repo from #162

OK, thanks.

This answers my question from above:

Are the images in different Markdown cells (but in the same notebook)?

The answer is: "yes".

So that's the problem: pandoc is called for each Markdown cell separately, and for each one it will start with image1 for images without or with repeated alt values.

This leads to multiple usages of image1 in one page, which leads to the mentioned warning and the duplication of images.

Sadly, I don't know a straightforward way to fix this.

As so many things, this will be easy to fix once #36 is done, but this will likely take a very long time.

Any further ideas how to fix this?

In the meantime, as a work-around, you should simply use a unique alt value for each image, which you should do anyway, for the sake of accessibility.