quarto-dev / quarto-vscode

Quarto extension for VS Code
https://marketplace.visualstudio.com/items?itemName=quarto.quarto
MIT License
112 stars 11 forks source link

Cell Magics #66

Closed JanPalasek closed 2 years ago

JanPalasek commented 2 years ago

Consider the following cell with cell magic %%javascript in the following example:

```{python}
#| output: false
%%javascript

// some JS Code

The cell works in qmd as expected. However it doesn't work in the Interactive Console. When you execute it, the interactive console is fed by this:

#| input: false
%%javascript

// some JS Code

I believe that the reason, why it doesn't work, is that comments are not allowed before cell magics. Cell magics need to be on the first line of the cell.

Note: The generated ipynb file also contains this and therefore cannot be executed.

I also encountered a second problem: Commands like "Run All Cells" don't work with Cell magics. It is because when running all cells, Quarto concatenates all code cells and then runs it. This is not viable with cell magic commands. It needs to be interpreted separately.

jjallaire commented 2 years ago

Thanks for reporting this! The first bit of the fix is here: https://github.com/quarto-dev/quarto-vscode/commit/642edba9e3e6d960dc73590b7153d3c75db3f868

Will investigate the other issues as well

jjallaire commented 2 years ago

Could you provide an example qmd file that results in:

(1) A generated ipynb that can't be executed; and

(2) Run All Cells not working as expected or yielding errors

When I try (2) with the %javascript example the ipython console handles it fine. I just need to a more realistic example as the starting point for troubleshooting.

jjallaire commented 2 years ago

I tried with this code and it works fine:

```{python}
%ls
%ls


I'm sure there is a case that's problematic, I just need a pointer to it. Thanks!
JanPalasek commented 2 years ago

Hi. Thanks for a quick response. The problem is cell magics, not line magics. Code cells with cell magics start with double % and modify the behaviour of the entire cell.

For example:

```{python}
#| output: asis
%%javascript

x = 1
if (x === 1) {
    alert("X is one");
}

I created a minimum example to reproduce this issue:

example.zip

If you run all code cells, it will try to run this code as a whole:

print(10)

#| output: asis
%%javascript

x = 1
if (x === 1) {
    alert("X is one");
}

print(15)

It doesn't work because code after %%javascript is expected to be interpreted as javascript and not python code. If you had evaluated it as a separate code cells, it would've worked.

There are a lot more cell magics and you can even have create your own. There is cell magic from ipycache to cache the contents of your cell. I have my own that interprets code using Jinja and allows you to write better Markdown with inline variables.

For example:

Example of using Jinja ![image](https://user-images.githubusercontent.com/26582151/186597653-e96ba01c-7919-4ec0-ab1d-769589720169.png)

Note: If you wanted to integrate something like that, it would be highly appreciated. It's quite powerful and allows you to generate e.g. dynamic tabsets quite easily (which is something you often do even in R, or my colleagues do, I work in Python):

Dynamic Tabset ![image](https://user-images.githubusercontent.com/26582151/186598423-b05826ef-96b1-4cd8-b115-7ed789902e3d.png)
jjallaire commented 2 years ago

Okay, thanks! Here is the next part of this fix (running cells one at a time): https://github.com/quarto-dev/quarto-vscode/commit/916b69cda1ff2ee1ed2f071f4fae2b7b1be505e0

In terms of the ipynb not being executable, that's a quarto-cli issue and I'm not 100% sure what to do about it (as the keep-ipynb feature is really more about debugging intermediate representations than producing a distributable notebook). issue filed here: https://github.com/quarto-dev/quarto-cli/issues/2123

JanPalasek commented 2 years ago

Thanks for the quick fix!