-
Notifications
You must be signed in to change notification settings - Fork 84
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
Add Myst-to-NB converter #82
Conversation
Or we could have a parser subclass: import nbformat as nbf
from myst_nb.parser import NotebookParser
from myst_nb.convert import myst_to_nb
class MystNbParser(NotebookParser):
supported = ("mystnb",)
def parse(self, inputstring, document):
ntbk = myst_to_nb(inputstring)
return super().parse(nbf.writes(ntbk), document) (it would be good here to allow |
FYI there is an edge case that should probably be raised with sphinx: if you have multiple documents with the same name but different extensions; sphinx will not complain but will read the same one multiple times, e.g. if you had
Only |
How do you see this usecase being used relative to writing in a |
Yes, I think these are three distinct document types. Although note, users could override this behaviour with source_suffix source_suffix = {
'.md': 'myst',
} Which would make all |
Note this is kind of an alternative to using the (a) at present you end up with different formatting/css relative to with the approach outlined in this PR, that is relatively trivial, and means we don't have to have any code (other than this function) that is different for notebooks and text documents; they'll be treated identically. |
Perhaps the default extension for these files could be |
In 5d50424 I have added the full round-trip conversion (well obviously sans code outputs). |
Actually, this comment: mwouts/jupytext#447 (comment) makes me think that marc would be happy for us to make myst a requirement for the ntbk <--> myst markdown sync. When people want to use pandoc markdown he just calls pandoc, and myst-parser is a more lightweight dependency than pandoc. Maybe @AakashGfude could give a look at @chrisjsewell 's code and see how tough it'd be to use that as the basis of a PR to jupytext? |
just to confirm - if the jupytext PR gets merged, then that will supercede this one right? |
(and, just another thought in case it seems relevant in the other PR, we could also keep the merging logic in this repository and have Jupytext use a function that myst_nb provides, or something like this...that might give us more iterative control over the code without needing to wait for upstream reviews in jupytext) |
It would yes
The problem with this is that all the testing is in jupytext. So any changes to the function might break these tests, but you wouldn't know until you tried to use the function in jupytext |
@chrisjsewell makes sense - and to be clear I prefer that this code is an upstream contribution to jupytext, I'm just a bit worried if we need to make some rapid changes to the codebase etc. |
also let's discuss the I added |
(happy to hop on a phone call to discuss this if that's a faster way to resolve this. I think that the "notebook format in myst" should match exactly what we have in the issue in this repository, unless the group decides otherwise, since that's what everybody agreed on the last time we discussed) |
I wouldn't say I have any strong preference either way TBH. Firstly though, I wanted to clarify the distinction, and note that if we do use Secondly, from a different perspective, if you're thinking of the file in terms of a notebook, isn't it more intuitive to use Thirdly, |
Also, it does feel a bit of a "fudge" that these directives aren't technically directives (i.e. that can be directly used by sphinx), but I don't see any better trade-off, to achieve a full round-trip capability? |
Yep, that was my intention all along. I just added the
I think these are both reasonable points - but the point is that we need to decide as a group what the syntax should be, we can't choose different names just at the time of a PR without getting feedback from others. I think these are reasonable options, and you make a good point about clobbering things like Another option is to treat this as a stop-gap for the sake of making progress, let marc know that the names may change but we're happy to make a PR to do this if it happens, then update this issue to have further discussion about how the proposed format should change. WDYT? |
👍
Oh yeh absolutely. I have actually written these functions to take the directive names as variables:
yes I'd be happy with this route |
The good thing about having now made this initial jupytext implementation, is that we have a whole suite of output |
The syntax here looks perfectly fine to my eyes: https://github.com/ExecutableBookProject/jupytext/blob/mystnb/tests/notebooks/mirror/ipynb_to_myst/Notebook%20with%20metadata%20and%20long%20cells.mystnb I don't have strong opinions on this point. |
The common pattern here is jupyter notebooks, and how they run code, not however RMarkdown runs code; we, and This is why _JUPYTER_LANGUAGES = ['R', 'bash', 'sh', 'python', 'python2', 'python3', 'javascript', 'js', 'perl',
'html', 'latex', 'markdown', 'pypy', 'ruby', 'script', 'svg',
'matlab', 'octave', 'idl', 'robotframework', 'spark', 'sql'] Lastly by having {r}, {python}, ... you are suggesting to the user that they can do something like:
and have that execute, which is just not the case, unless we completely re-write myst-nb and scrap notebook round-tripping and jupyer-cache. |
I agree. Jupyter notebooks is the right mental model for authors, one kernel per notebook. I imagine most people will start off authoring in ipynb anyway, and then gradually start to use text based source as they seek extra features. |
I agree that thinking in terms of notebooks is a good way to think about how the blocks are executed but I don't think the syntax should be tied to |
But the converse is, if I had converted from a notebook then seeing |
Personally, I agree with you both in terms of I'd be fine with either what about why do we not like |
Yeh I absolutely take your point, its just trying to get the balance right. Remember that, since jupytext now round-trips both RMarkdown and MyST-NB, we do have a ready-made conversion tool from RMarkdown 😄
|
I think |
thanks for the quick discussion and turnaround on this one all, much appreciated! it definitely does make me more responsive when I'm not supposed to leave the house 😆 |
I hear what you're saying about Rmarkdown, @choldgraf , although perhaps it's possible to overstate the number of would-be authors who are coming from that environment. I imagine that the great majority will be notebook users hoping to soup up the presentation of their results, while knowing little about Rmarkdown. (Like me 😁) I suspect most (but not all) Rmarkdown users are R users, who have good tools already in terms of EBP substitutes. Anyways, +1 for |
Hey all - another decision-point has come up in @chrisjsewell's PR to jupytext: Should the extension coming out of jupytext be
My preference as you can guess is for plain old |
Looks like you got your preference: mwouts/jupytext@62351f4 |
Yeh I can't be bothered to argue about it lol. It does mean that I'll probably put the LSP on the back-burner though. Because, if there is no way to distinguish myst from md, then it doesn't really work. |
Also these files won't work directly with the @AakashGfude will also need to use it in some manner in #55 to work out which markdown files to convert/execute/cache |
In that case I can definitely be bothered to argue about it. In terms of the user interface, this is hugely important. I can tell you this as someone who has been authoring in jupinx for a long time. The lack of editor support for the source format is absolutely the first thing I would change if I could. I think user confusion between |
Sorry I'm late to the party on this --- clearly I didn't understand all the implications --- but should we put a hold on mwouts/jupytext#458 until this is decided? |
Given what @chrisjsewell mentioned I think I'd be fine if we used I am also happy to hop on a call to discuss this if that's helpful to hash out the details. |
No, that's probably not necessary. Note that, with the current PR state, if you have jupytext installed and this at the top of your file: ---
jupytext:
text_representation:
format_name: myst
format_version: '0.7'
jupytext_version: 1.4.0+dev
kernelspec:
display_name: Python 3
language: python
name: python3
--- Then any of the following extensions will open directly as notebooks (and changes saved back to them): |
I think theres a way forward on this, so mwouts/jupytext#458 can definitely be merged. Then @choldgraf can decide if you want to work out how to make the default pairing (if going from |
Let's keep it mnb for now, and see how things go. |
Yeh cool cool, if you mention that to mwouts in the PR then, and hopefully we can get a jupytext release with this code in soon, to utilise with #82 (comment) |
done 👍 |
Cheers 😁 |
Thanks all!! |
Nice work! Can I just say it's great working in a team that is open to debate and dialogue on these issues. The project will be better off for it. |
MyST format is now merged into jupytext 🎉 https://github.com/mwouts/jupytext/releases/tag/1.4.1 |
This PR introduces a function for converting text documents, written in the myst format, to a notebook. It could be used in the parser and in #55 to convert these documents to notebooks before executing/reading. See the test input/output for an example.
I would envisage this could be utilised by also adding
myst
to https://github.com/ExecutableBookProject/MyST-NB/blob/4c3f36b884086878dc0af5dfcfd735b98ebbbacf/myst_nb/parser.py#L27This would denote files with the
.myst
extension as ones that should be parsed as notebooks, as opposed to normal.md
files, then in https://github.com/ExecutableBookProject/MyST-NB/blob/4c3f36b884086878dc0af5dfcfd735b98ebbbacf/myst_nb/parser.py#L35we would check the documents extension and apply this function, instead of
nbf.reads
, before proceeding with the rest of the process.This addresses to a large extent #12