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

Indenting executable codeblocks makes them malfunction #9970

Open
GuillaumeDehaene opened this issue Jun 12, 2024 · 5 comments
Open

Indenting executable codeblocks makes them malfunction #9970

GuillaumeDehaene opened this issue Jun 12, 2024 · 5 comments
Labels
bug Something isn't working jupyter
Milestone

Comments

@GuillaumeDehaene
Copy link

Bug description

Normal markdown code supports nesting environments into one-another using indentation. For example, lists can be nested. Paragraphs can be nested into a list bullet-point. Also, indentation can be used to mark a verbatim block.

However, indenting executable code blocks makes them malfunction (for jupyter).

Steps to reproduce

This minimal example compiles to very weird markdown. The resulting html is weird. As far as I can tell, this is due to correct parsing of the weird markdown.

NB: this is a minimal example with a stupid output. The issue persists if using a Markdown(tabulate(...)) example or a matplotlib figure.

---
execute:
    echo: false

format: markdown
---

# Minimal example

```{python}
print("- Foo")
```

    ```{python}
    print("- Foo")
    ```

- Nesting list

    - Foo

- Nesting list:

    ```{python}
    print("- Foo")
    ```

After compilation

# Minimal example

:::: {.cell execution_count="1"}
::: {.cell-output .cell-output-stdout}
    - Foo
:::
::::

    ::: {.cell execution_count=2}

    ::: {.cell-output .cell-output-stdout}
    ```
    - Foo
    ```
    :::
    :::

-   Nesting list

    -   Foo

-   Nesting list:

    ::: {.cell execution_count=3}

    ::: {.cell-output .cell-output-stdout} \`\`\`

    -   Foo \`\`\` ::: :::

Expected behavior

I'm not sure what the expected behavior should be, without breaking everything and making everything super complex.

  • For indentation outside of any context (first half of the example), I'm not sure if there is even any solution that makes sense:

    • maintaining indentation results in a verbatim code block, which is very weird
    • changing the markdown produced by execution sounds very complicated for very little benefit
    • can verbatim blocks from indentation be turned off in pandoc? I don't think so?
    • the simplest might be to write in the documentation that indenting produces this weird behavior?
  • For indentation inside of a list, like in the second half of the example, I would expect the behavior to be that indentation is preserved and produces correct markdown. I think the issue here is only that the markdown is mangled somehow.

Actual behavior

  • indentation outside of context: produces ugly verbatim code
  • indentation inside of context of list: mangled output, probably due to incorrect parsing of the div delimiters

Your environment

  • IDE: vscode
  • OS: windows 11

Quarto check output

Quarto 1.5.45
[>] Checking versions of quarto binary dependencies...
Pandoc version 3.2.0: OK
Dart Sass version 1.70.0: OK
Deno version 1.41.0: OK
Typst version 0.11.0: OK
[>] Checking versions of quarto dependencies......OK
[>] Checking Quarto installation......OK
Version: 1.5.45
Path: C:\Users\Guillaume\AppData\Local\Programs\Quarto\bin
CodePage: 1252

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

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

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

[>] Checking Python 3 installation....(None)

  Unable to locate an installed version of Python 3.
  Install Python 3 from https://www.python.org/downloads/

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

  Unable to locate an installed version of R.
  Install R from https://cloud.r-project.org/

PS D:\Desktop\Dojo\quarto_bug_report_listings>

I'm not sure why python is not seen but everything compiles as it should:

Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32

@GuillaumeDehaene GuillaumeDehaene added the bug Something isn't working label Jun 12, 2024
@cscheid
Copy link
Collaborator

cscheid commented Jun 12, 2024

Unfortunately that's a limitation in our Jupyter setup. Code cells are not part of markdown; they're executed as separate cells in the .ipynb file, and so they need to be "top level".

Yes, this is a bug, but it's a pretty foundational limitation that we won't be able to fix anytime soon.

@cscheid cscheid added this to the Future milestone Jun 12, 2024
@GuillaumeDehaene
Copy link
Author

I honestly expected as much. Two small questions:

  1. Should I add a remark to the documentation of executable code blocks to say that they don't work when indented?
  2. Have you checked out how weird the markdown is in in the context of the list? From the outside, it looks like there's also a small bug in that generation. If so, it might be possible to fix that bug cheaply, which would hopefully repair indented executable codeblocks in that context?

For reference, I'm pasting the relevant section of the resulting markdown:

normal context

:::: {.cell execution_count="1"}
::: {.cell-output .cell-output-stdout}
    - Foo
:::
::::

list context

    ::: {.cell execution_count=3}

    ::: {.cell-output .cell-output-stdout} \`\`\`

    -   Foo \`\`\` ::: :::

There's a bunch of small differences

@mcanouil
Copy link
Collaborator

The issue described here is only for engine: jupyter.
knitr and julia engines are working quite differently but ot necessarily as expected.

Quarto documentHTML
---
execute:
    echo: false
engine: knitr
format: markdown
---

# Minimal example

```{r}
print("- Foo")
```

    ```{r}
    print("- Foo")
    ```

- Nesting list

    - Foo

- Nesting list:

    ```{r}
    print("- Foo")
    ```
---
execute:
  echo: false
toc-title: Table of contents
---

# Minimal example

:::: cell
::: {.cell-output .cell-output-stdout}
    [1] "- Foo"
:::
::::

    ::: {.cell}
    ::: {.cell-output .cell-output-stdout}

    ```
    [1] "- Foo"
    ```


    :::
    :::

-   Nesting list

    -   Foo

-   Nesting list:

    ::: {.cell} ::: {.cell-output .cell-output-stdout}

        [1] "- Foo"

    ::: :::
---
execute:
    echo: false
engine: julia
format: markdown
---

# Minimal example

```{julia}
print("- Foo")
```

    ```{julia}
    print("- Foo")
    ```

- Nesting list

    - Foo

- Nesting list:

    ```{julia}
    print("- Foo")
    ```
---
execute:
  echo: false
toc-title: Table of contents
---

# Minimal example

:::: {.cell execution_count="1"}
::: {.cell-output .cell-output-stdout}
    - Foo
:::
::::

    ```{julia}
    print("- Foo")
    ```

-   Nesting list

    -   Foo

-   Nesting list:

    ``` {julia}
    print("- Foo")
    ```

@cderv
Copy link
Collaborator

cderv commented Jun 12, 2024

Interesting...

I know we deal with indent in knitr, but it seems we don't correctly write the cell output div with no indent as they should be.

Fenced div only works when no starting indent

Just to clarify, the support for indented code cell for knitr engine is not a Quarto thing, this is a feature of knitr inherit by Quarto.

Julia engine (from Quarto) does not seem to support it which is what I would have expected.

@GuillaumeDehaene
Copy link
Author

Here is an update:

for engine: jupyter, using the output: asis option (see here) fixes the issue by removing the incorrect wrapping divs

In 1.6 , another possibility consists in using the contents shortcode: see here

Since this is probably too expensive fix for to little gain, I'm guessing updating the docs to specify this is a limitation would be appropriate.
Should I add a section to all engine pages:

describing this behavior and the two possible fixes?

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

No branches or pull requests

4 participants