Replies: 4 comments 28 replies
-
My thought is this is an all or nothing option: the files either map 1:1 markdown <-> notebook, or they are not associated with notebooks at all. (2) is the better option IMO |
Beta Was this translation helpful? Give feedback.
-
Also linking #143 with the same question being discussed. I think the main relevant point from that discussion is that it would be possible to support code cells nested anywhere, if jupytext conversion was based on myst-parser as well as sphinxcontrib-rst2myst and not only markdown-it-py, but this has unreasonably high implementation costs. Out of the options above, I think (2) (a.k.a. executablebooks/MyST-NB#265) is indeed the most reasonable since it's completely general, and doesn't break user assumptions about execution order etc. |
Beta Was this translation helpful? Give feedback.
-
Another remark that I think is useful here is the meaning of 1-1 mapping myst ↔ ipynb. This, to the best of my understanding is a shorthand to the requirement that the following two sequences of actions produce the same result:
and
This is more stringent than only requiring bidirectional conversion myst ↔ ipynb with jupytext. If that was the only constraint, then I believe it would be possible to support nested code cells with a lower complexity than dragging the complete stack into jupytext. |
Beta Was this translation helpful? Give feedback.
-
Sorry to jump in later in the discussion on this. A comments for Would it be a reasonable assumption that From the
Alternative Build Approach: Another
during the build process. The The downside as I see it from this approach is it would mix |
Beta Was this translation helpful? Give feedback.
-
This has been discussed elsewhere (jupyter-book/jupyter-book#700, executablebooks/sphinx-exercise#18 for example) so moving here to have a single place to discuss this topic.
Current situation
Currently, executable code must exist in the form of a
{code-cell}
at the top level of a markdown file, or in the cell of a Jupyter Notebook. This is because we want a 1-to-1 mapping of text files <--> ipynb file via Jupytext, and since Jupyter Notebooks have a flat, top-level list of code cells, that is the only pattern that we support.The problem
The problem that others have noted is that you often want to embed the results of running code inside other parts of your content. The most common example that has come up is to put code cells / outputs inside of an admonition. For example:
The above pattern is not possible currently, because
{code-cell}
needs to be a top-level item, mimicking the structure of a Jupyter Notebook (which has no concept of code cells nested in other cells).So one question we'd need to answer to support this is: how would the above code block be translated into an
ipynb
file? Right now I do not believe that it is possible with any existing tool.A few potential solutions
Here are some ideas I can think of that we could discuss
admonition: note
,admonition-body: Some body text
,admonition-footer
, and when converted to markdown it would use this metadata to wrap the resultingcode-cell
in a{note}
admonition.glue
, a python function that tags a variable with a name for insertion. Others have pointed out this feels too cumberson, and is also python-specific. Perhaps making it easier to insert code cells / outputs from other pages will make this a workable solution.ipynb
file (it would just be copied as raw text or something).{execute}`print("hi")`
would be run when the book was built, but would be meaningless in the context of a Jupyter interface.nbclient
for our execution, and this doesn't support anything like what I describe.papermill
and writing a post-execution hook that would run these blocks of code after executing all of the cells on the page.nbclient
if we had something like Function hooks when executing cells / notebooks? jupyter/nbclient#81sphinx-exercise
could work around this behavior (maybe by adopting its own special cell metadata?) and then document how other extensions could do the same thing.I don't think any of these solutions feels obviously correct to me. Personally if the machinery were there to make 3 possible, I think that's the one I'd prefer since it is closest to what RMarkdown does, and people seem to quite like that feature.
Maybe others have ideas for supporting this through other means - I would love to hear your thoughts.
Beta Was this translation helpful? Give feedback.
All reactions