Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the number of backticks used for Jupyter notebook code cell be specified? #8919

Closed
jackgerrits opened this issue Feb 28, 2024 · 15 comments
Closed
Assignees
Labels
bug Something isn't working jupyter
Milestone

Comments

@jackgerrits
Copy link

jackgerrits commented Feb 28, 2024

Bug description

I have a code cell in a notebook which contains a string with a markdown code block in it. This breaks the rendering of the outputted markdown content. Is it possible to configure the number of backticks that are used when outputting a code cell form a notebook to markdown? (In my case docusaurus, so mdx)

Steps to reproduce

The following would be the contents of the cell:

message = """
content
```python
print("Hello")
```
"""

Which gets rendered is:

```python
message = """
content
```python
print("Hello")
```
"""
```

This does not render correctly

Expected behavior

No response

Actual behavior

No response

Your environment

  • CLI, MacOS 14.3.1

Quarto check output

Quarto 1.4.549
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.1.11: OK
      Dart Sass version 1.69.5: OK
      Deno version 1.37.2: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.4.549
      Path: /Applications/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: (not installed)
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Tex:  (not detected)

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.11.7
      Path: /Users/jackgerrits/w/autogen/.venv/bin/python3
      Jupyter: 5.7.1
      Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........(None)

      Unable to locate an installed version of R.
      Install R from https://cloud.r-project.org/
@jackgerrits jackgerrits added the bug Something isn't working label Feb 28, 2024
@mcanouil
Copy link
Collaborator

mcanouil commented Feb 28, 2024

That's unrelated to Quarto.
It's Markdown, the same will happen on any Markdown document/services such as GitHub.

Note that your example has no code cells. Code cells describe a Markdown code block where its8 content can be used for computation.

See below.

RPython
````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: knitr
---

This is a reproducible Quarto document.

```{r}
x <- c(1, 2, 3, 4, 5)
y <- c(1, 4, 9, 16, 25)

plot(x, y)
```

![An image](https://placehold.co/600x400.png)

The end.
````
````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: jupyter
---

This is a reproducible Quarto document.

```{python}
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)
plt.show()
```

![An image](https://placehold.co/600x400.png)

The end.
````

@mcanouil
Copy link
Collaborator

For more help:

Could you share a small self-contained "working" (reproducible) example to work with, i.e., a complete Quarto document or a Git repository? Thanks.


You can share a self-contained "working" (reproducible) Quarto document using the following syntax, i.e., using more backticks than you have in your document (usually four ````).
See https://quarto.org/bug-reports.html#small-is-beautiful-aim-for-a-single-document-with-10-lines.

If you have multiple files (and if it is absolutely required to have multiple files), please share as a Git repository.

RPython
````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: knitr
---

This is a reproducible Quarto document.

```{r}
x <- c(1, 2, 3, 4, 5)
y <- c(1, 4, 9, 16, 25)

plot(x, y)
```

![An image](https://placehold.co/600x400.png)

The end.
````
````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: jupyter
---

This is a reproducible Quarto document.

```{python}
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)
plt.show()
```

![An image](https://placehold.co/600x400.png)

The end.
````

Additionally and if not already given, please share the output of quarto check within a code blocks (i.e., using three backticks ```txt), see https://quarto.org/bug-reports.html#check.

@mcanouil mcanouil added the needs-repro Issues that are blocked until reporter provides an adequate reproduction label Feb 28, 2024
@jackgerrits
Copy link
Author

jackgerrits commented Feb 28, 2024

Sorry my example was not very clear. I am using quarto render in a docusaurus website to render jupyter notebooks to mdx.

What happens is when the notebook contains a code cell, the produced mdx file contains the markdown representation of that code cell using 3 backticks as the fence.

The issue is that the code cell itself contained a string with 3 backticks too, which breaks the rendering. Normally, I would make the outer codefence use 4 or more backticks to resolve this but quarto (or pandoc?) is the one doing this translation I can't seem to do that.

@mcanouil
Copy link
Collaborator

I am using quarto render in a docusaurus website to build

I know, I saw "(In my case docusaurus, so mdx)", but knowing this does not make your example more reproducible.

@jackgerrits
Copy link
Author

Sent the message too soon - see the updated content above.

@jackgerrits
Copy link
Author

jackgerrits commented Feb 28, 2024

If this is the cell in the notebook:
image
Then the rendered content I am getting is:

```python
message = """
Content
```python
print("Hello, world!")
```
"""
```

It would be great if there was some way to specify an option saying use 4 backticks for cell code blocks to avoid this issue. Then ideally I could get something like the following out of quarto render:

````python
message = """
Content
```python
print("Hello, world!")
```
"""
````

@cscheid
Copy link
Collaborator

cscheid commented Feb 28, 2024

Yeah, this is definitely a bug on our side.

@cscheid cscheid removed the needs-repro Issues that are blocked until reporter provides an adequate reproduction label Feb 28, 2024
@cscheid cscheid added this to the v1.5 milestone Feb 28, 2024
@cscheid cscheid self-assigned this Feb 28, 2024
@jackgerrits
Copy link
Author

I think its related to docusaurus because when I was trying to get a standalone repro (just jupyter -> markdown) it was correctly using 4 instead of 3 backticks in the above example. But in the more complex situation of file being inside of a docusaurus site I was seeing the issue

@jackgerrits
Copy link
Author

jackgerrits commented Feb 28, 2024

For the markdown only example that did work I get the following output:

pandoc
  to: >-
    markdown_strict+raw_html+all_symbols_escapable+backtick_code_blocks+fenced_code_blocks+space_in_atx_header+intraword_underscores+lists_without_preceding_blankline+shortcut_reference_links
  output-file: repro.md
  standalone: true
  default-image-extension: png

Output created: repro.md

But the docusaurus version outputs:

pandoc
  to: >-
    markdown_strict+raw_html+all_symbols_escapable+backtick_code_blocks+fenced_code_blocks+space_in_atx_header+intraword_underscores+lists_without_preceding_blankline+shortcut_reference_links+autolink_bare_uris+emoji+footnotes+gfm_auto_identifiers+pipe_tables+strikeout+task_lists+tex_math_dollars+pipe_tables+tex_math_dollars+header_attributes+raw_html+all_symbols_escapable+backtick_code_blocks+fenced_code_blocks+space_in_atx_header+intraword_underscores+lists_without_preceding_blankline+shortcut_reference_links
  output-file: jupyter-code-executor.mdx
  standalone: true
  default-image-extension: png
  wrap: none
  html-math-method: webtex

metadata
  quarto-custom-format: docusaurus
  title: Jupyter Code Executor

Output created: jupyter-code-executor.mdx

Does quarto-custom-format: docusaurus affect things?

@cscheid
Copy link
Collaborator

cscheid commented Feb 28, 2024

It does, but you don't need that. Here's another instance of the issue:

$ cat bug.ipynb
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d02a6fa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"\"\"\n",
    "Content\n",
    "```python\n",
    "print(\"Hello, world!\")\n",
    "```\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
$ quarto convert bug.ipynb
$ cat bug.qmd
---
jupyter: python3
---

```{python}
message = """
Content
```python
print("Hello, world!")
```
"""
```

That's already broken.

@jackgerrits
Copy link
Author

I can confirm that rendering the same notebook and just ensuring the target format is md instead of the default mdx it uses the correct code fence (4 backticks when it needs it)

quarto render docs/jupyter-code-executor.ipynb --to md

pandoc
  to: >-
    markdown_strict+raw_html+all_symbols_escapable+backtick_code_blocks+fenced_code_blocks+space_in_atx_header+intraword_underscores+lists_without_preceding_blankline+shortcut_reference_links
  output-file: jupyter-code-executor.md
  standalone: true
  default-image-extension: png

metadata
  title: Jupyter Code Executor

Output created: jupyter-code-executor.md

@jackgerrits
Copy link
Author

Oh awesome, that repro is much better then

@jackgerrits
Copy link
Author

jackgerrits commented Feb 28, 2024

With your example if you run it like this:

quarto render bug.ipynb --to md

It works, but if you convert it like you said it doesn't work.

@cscheid
Copy link
Collaborator

cscheid commented Feb 28, 2024

Funnily enough, it's two bugs. I fixed the first one in a branch and yet the following is still broken:

$ quarto convert bug.ipynb
...
$ cat bug.qmd
---
jupyter: python3
---

````{python}
message = """
Content
```python
print("Hello, world!")
```
"""
````
$ quarto render bug.ipynb --to docusaurus-md
$ cat bug.mdx



```python
message = """
Content
```python
print("Hello, world!")
```
"""
```

@cscheid
Copy link
Collaborator

cscheid commented Feb 28, 2024

Fixed on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jupyter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants