jupyter-book / jupyterlab-myst

Use MyST Markdown directly in Jupyter Lab
https://jupyter-book.github.io/jupyterlab-myst/
BSD 3-Clause "New" or "Revised" License
141 stars 17 forks source link

Using `code-cell` in a markdown cell within `jupyterlab-mystjs` #73

Open mmcky opened 1 year ago

mmcky commented 1 year ago

Describe the bug

The code-cell directive from jupyter-book will be rendered as an executable component in JupyterBook when using myst:markdown files.

However in the jupyterlab-mystjs context these blocks become syntax highlighted code blocks within the markdown cells.

Example:

It might be nice for consistency if jupyerlab-mystjs was able to transform a code-cell such as

We would like to import pandas

```{code-cell} python
import pandas as pd

which we can now use in our notebook



split the markdown cell and added it in as a `code-cell` in the `jupyter` context to become:

<img width="358" alt="Screenshot 2023-01-26 at 2 45 39 pm" src="https://user-images.githubusercontent.com/8263752/214754440-a121e7f3-9f84-4d0f-958f-cc224ddd28f4.png">

### Reproduce the bug

Use the example snippet above and it returns

<img width="358" alt="Screenshot 2023-01-26 at 2 48 32 pm" src="https://user-images.githubusercontent.com/8263752/214754752-7c7f777a-ee1e-418b-9a4a-810a401ee604.png">

### List your environment

_No response_
welcome[bot] commented 1 year ago

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:

agoose77 commented 1 year ago

Hmm, I won't speak for @rowanc1 but I think this would be out of scope for the Jupyter integration. IIRC our integration in JupyterLab sits at the notebook level, so we customise the view but use the same model :/

rowanc1 commented 1 year ago

Previously @agoose77 you were mentioning how to transform the markdown into a notebook. And that is a custom something in JupyterLab (not the view), i.e. something similar to jupytext.

Is that the right place to do this transformation?

rowanc1 commented 1 year ago

I have put together a quick video (1.5min) of how things are working for me, can you describe the workflow a bit more that you are expecting if this isn't what you are wanting @mmcky?!

https://loom.com/share/d6996cbbd7ac464b8fe27cd416237af5

mmcky commented 1 year ago

@rowanc1 that would work well and is a good solution.

I was suggesting that if you copy and paste some myst markdown into a markdown cell that it might be able to split the cell on code-cell directives but the solution you propose by opening the original md file is a great approach.

mmcky commented 1 year ago

I have put together a quick video (1.5min) of how things are working for me, can you describe the workflow a bit more that you are expecting if this isn't what you are wanting @mmcky?!

https://loom.com/share/d6996cbbd7ac464b8fe27cd416237af5

Will this solution require the jupytext headers in the md file such as

---
jupytext:
  text_representation:
    extension: .md
    format_name: myst
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---
rowanc1 commented 1 year ago

I think if you have jupytext enabled, it actually does cut the cell on the code cell, but only on save/re-open, not as you are working in the notebook. It is a bit strange! :)

At this point the frontmatter is the only way I know how to get it working. After testing, it looks like at least this is needed:

---
jupytext:
  formats: ipynb,md:myst
---

I am sure there are other ways to do it that I don't know about, but at the least letting jupytext know that you are working with myst seems important.

How were you working before with these markdown notebooks? Did you open them in JupyterLab or use Jupytext at all?

mmcky commented 1 year ago

@rowanc1 I just updated our QuantEcon manual to document the header requirement for markdown files. I don't think this is a big issue (particularly in the beginning). It makes sense that jupytext needs some metadata to know about the contents of the markdown and which kernel to connect to.

How were you working before with these markdown notebooks? Did you open them in JupyterLab or use Jupytext at all?

To be honest the most common way is for folks to go from ipynb to myst:md and the headers are there in that conversion process automatically. It only became an issue yesterday for us as someone was converting a raw markdown file.

mmcky commented 1 year ago

It would be amazing though to be able to use myst:md files directly with jupyterlab + jupyterlab-mystjs as we will only commit md files in the GitHub repo context.

agoose77 commented 1 year ago

You can work exclusively on a myst:md file using jupytext without creating any ipynb. I think you do need the headers though

rowanc1 commented 1 year ago

This has been updated a bit in the v1.1.3 release, which now parses and renders code-cell directives in the markdown cells, these are not executed, and if they are flat, round-tripping them through jupytext will result in a new code cell being created. I think this is ok for now - but still something to keep an eye on.

@mmcky would love your feedback on the latest release at some point. :)