MyST-NB icon indicating copy to clipboard operation
MyST-NB copied to clipboard

Unable to add more than 1 caption in notebook

Open AlexAndorra opened this issue 5 years ago • 10 comments

Bug description

Hi team, and thanks a lot for developing this great tool! I'm currently writing a book with @OriolAbril and am unable to generate captions for figures in notebooks. More precisely, adding one caption works, but adding 2 captions or more makes sphinx-build . build -b html fail and stops the HTML compilation:

Une exception a été levée :
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/docutils/nodes.py", line 1439, in set_name_id_map
    self.nametypes[name] = explicit
TypeError: 'dict_items' object does not support item assignment

To be clear, here is how I add a caption (of course, tell me if this is a wrong way):

{code-cell} ipython3
---
render:
  figure:
    caption: "This is a test for a plot caption"
    name: fig:test-caption
---
plt.plot(x, y, "o");

The full traceback (see below) made me think that this was a myst-nb issue, but let me know if I'm mistaken and this ticket should be transferred on another repo.

To Reproduce

I setup this small repo to reproduce the error. Steps to reproduce the behavior:

  1. Install the virtual env with conda env create -f environment.yml
  2. Go to code/source (that's where the conf.py file is)
  3. Run sphinx-build . build -b html
  4. You should see the error I posted above
  5. If you delete the generated build and jupyter cache folders and remove one caption from index.md, it should build successfully again

Environment

  • Python Version: 3.8.6
  • jupyter-sphinx: 0.3.2
  • jupytext: 1.6.0
  • myst-nb: 0.10.1
  • myst-parser: 0.12.10
  • sphinx: 3.3.0
  • sphinx-book-theme: 0.0.37
  • Operating System: Mac OS Mojave

Full traceback

# Sphinx version: 3.3.0
# Python version: 3.8.6 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.2
# Last messages:
#   recherche des fichiers périmés...
#   aucun résultat
#   environnement de sérialisation...
#   fait
#   vérification de la cohérence...
#   fait
#   document en préparation...
#   fait
#   écriture... [ 10%] .jupyter_cache/executed/d9f95c0e41a4190bf8d726ca9359f939/base
#   écriture... [ 20%] .jupyter_cache/executed/e7a99ed60ee01a53cca4ff71b920377c/base
# Loaded extensions:
#   sphinx.ext.mathjax (3.3.0) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/ext/mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinxcontrib/applehelp/__init__.py
#   sphinxcontrib.devhelp (1.0.2) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinxcontrib/devhelp/__init__.py
#   sphinxcontrib.htmlhelp (1.0.3) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinxcontrib/htmlhelp/__init__.py
#   sphinxcontrib.serializinghtml (1.1.4) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinxcontrib/serializinghtml/__init__.py
#   sphinxcontrib.qthelp (1.0.3) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinxcontrib/qthelp/__init__.py
#   alabaster (0.7.12) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/alabaster/__init__.py
#   sphinxcontrib.bibtex (unknown version) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinxcontrib/bibtex/__init__.py
#   sphinx_togglebutton (0.2.2) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx_togglebutton/__init__.py
#   myst_nb (0.10.1) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/myst_nb/__init__.py
#   sphinx_book_theme (unknown version) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx_book_theme/__init__.py
#   pydata_sphinx_theme (unknown version) from /Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/pydata_sphinx_theme/__init__.py
Traceback (most recent call last):
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/cmd/build.py", line 280, in build_main
    app.build(args.force_all, filenames)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/application.py", line 352, in build
    self.builder.build_update()
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 297, in build_update
    self.build(to_build,
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 361, in build
    self.write(docnames, list(updated_docnames), method)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 535, in write
    self._write_serial(sorted(docnames))
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 542, in _write_serial
    doctree = self.env.get_and_resolve_doctree(docname, self)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/environment/__init__.py", line 539, in get_and_resolve_doctree
    self.apply_post_transforms(doctree, docname)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/environment/__init__.py", line 585, in apply_post_transforms
    transformer.apply_transforms()
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/transforms/__init__.py", line 87, in apply_transforms
    super().apply_transforms()
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/docutils/transforms/__init__.py", line 171, in apply_transforms
    transform.apply(**kwargs)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/sphinx/transforms/post_transforms/__init__.py", line 44, in apply
    self.run(**kwargs)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/myst_nb/render_outputs.py", line 112, in run
    output_nodes = renderer.cell_output_to_nodes(self.env.nb_render_priority)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/myst_nb/render_outputs.py", line 187, in cell_output_to_nodes
    output_nodes.extend(self.render(mime_type, output, idx))
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/myst_nb/render_outputs.py", line 296, in render
    nodes = self.create_render_image(mime_type)(output, index)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/myst_nb/render_outputs.py", line 470, in _render_image
    self.add_name(figure_node, name)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/myst_nb/render_outputs.py", line 232, in add_name
    self.document.note_explicit_target(node, node)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/docutils/nodes.py", line 1493, in note_explicit_target
    self.set_name_id_map(target, id, msgnode, explicit=True)
  File "/Users/alex_andorra/opt/anaconda3/envs/bayes_book/lib/python3.8/site-packages/docutils/nodes.py", line 1439, in set_name_id_map
    self.nametypes[name] = explicit
TypeError: 'dict_items' object does not support item assignment

AlexAndorra avatar Dec 24 '20 10:12 AlexAndorra

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

welcome[bot] avatar Dec 24 '20 10:12 welcome[bot]

Hi @AlexAndorra

can you try putting quotations around the name, since it is possibly reading name: fig:test-caption as {"name": {"fig": "test-caption"}}:

{code-cell} ipython3
---
render:
  figure:
    caption: "This is a test for a plot caption"
    name: "fig:test-caption"
---
plt.plot(x, y, "o");

chrisjsewell avatar Dec 24 '20 10:12 chrisjsewell

Hi @chrisjsewell, and thanks for your quick answer! I just tried that and unfortunately get the same error as above 😕 Do want me to try something else?

AlexAndorra avatar Dec 24 '20 11:12 AlexAndorra

ok thanks, hmm if you bear with me, I will try to look over the weekend. If you could perhaps attach/copy here a full notebook from which I will definitely be able to reproduce the error

chrisjsewell avatar Dec 24 '20 12:12 chrisjsewell

if you bear with me, I will try to look over the weekend

Yes of course, thank you!

If you could perhaps attach/copy here a full notebook from which I will definitely be able to reproduce the error

I setup this small repo to reproduce the error:

  1. Install the virtual env with conda env create -f environment.yml
  2. Go to code/source (that's where the conf.py file is)
  3. Run sphinx-build . build -b html
  4. You should see the error I posted above
  5. If you delete the generated build and jupyter cache folders and remove one caption from index.md, it should build successfully again

Does it look enough to you?

AlexAndorra avatar Dec 24 '20 13:12 AlexAndorra

Hey @chrisjsewell ! Just checking in to see if my previous comment has enough info for you to reproduce, or if you need anything else? Thanks in advance and take care 🖖

AlexAndorra avatar Dec 29 '20 15:12 AlexAndorra

Also worth noting that the repo linked above is a minimal example to reproduce the error, it only has a markdown file, a conf.py file and an environment.yml file. It is not the repo of our book.

OriolAbril avatar Dec 29 '20 18:12 OriolAbril

Hey yeh I haven't got round to looking at it just yet I'm afraid, but that should be fine thanks 👍

chrisjsewell avatar Dec 29 '20 18:12 chrisjsewell

Related: https://github.com/executablebooks/MyST-NB/issues/285

agoose77 avatar Jun 14 '22 11:06 agoose77

@agoose77 did you want to link to https://github.com/executablebooks/MyST-NB/issues/374? Or comment there with the link to this issue?

OriolAbril avatar Jun 14 '22 15:06 OriolAbril