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

Orgdown code blocks aren't exported as code blocks to Jupyter #8236

Closed
morgandavidson opened this issue Aug 20, 2022 · 8 comments
Closed

Orgdown code blocks aren't exported as code blocks to Jupyter #8236

morgandavidson opened this issue Aug 20, 2022 · 8 comments

Comments

@morgandavidson
Copy link

I'm using Pandoc 2.19.1 on Emacs 28.1 through Pandoc-mode 2.31. OS is Ubuntu 20.04.

When converting a notebook from an org file to an ipynb file the notebook is converted into a single markdown cell instead of multiple cells containing markdown and code.

For example the following org file :

* Load HealthCoach dataset
  - Parse date column as dates
  - Use data column as index
  - [[../../../Logbook/Health/Measurements/weight.org][Weight DB]]
  #+begin_src jupyter-python :session py :display plain 
  import pandas as pd 
  df = pd.read_csv('../../../Logbook/Health/Measurements/weight.csv', parse_dates=['Date'],index_col=0)
  #+end_src

Will be converted into a single markdown cell in ipynb, instead of 1 markdown cell for the text part and 1 code cell for the code part.

@morgandavidson morgandavidson changed the title Orgdown code blocks aren't exported as code blocs to jupyter Orgdown code blocks aren't exported as code blocks to Jupyter Aug 20, 2022
@tarleb
Copy link
Collaborator

tarleb commented Aug 21, 2022

Pandoc doesn't if the code should be part of the text cell or should be treated as a separate code cell. Currently all code blocks with class code will be converted to code cells. You can use a Lua filter like the below to add the class:

function CodeBlock (cb)
  if cb.classes[1] == 'jupyter-python' then
    cb.classes:insert 'code'
    return cb
  end
end

We could make this the default if there is any agreed-upon way to mark code cells in orgmode.

@morgandavidson
Copy link
Author

morgandavidson commented Aug 21, 2022

Thanks for the tip. Indeed it would be great to make this default. Actually, the standard way of adding code blocks in org-mode is to start with #+BEGIN_SRC and to end with #+END_SRC, as I did in my example. See Working with Source Code from Org-Mode manual. Do you need more info?

@tarleb
Copy link
Collaborator

tarleb commented Aug 21, 2022

I guess my question is if there's a standard way of telling apart src blocks that are code cells and src blocks that are part of a text cell. Your example uses jupyter-python as the block's language. Is that a general convention that's documented somewhere?

@morgandavidson
Copy link
Author

morgandavidson commented Aug 21, 2022

In my example #+BEGIN_SRC and #+END_SR indicate that what is between these two terms is a code block. Whereas jupyter-python means that the code is to be interpreted with a Jupyter kernel.

So what Pandoc should understand is that everything below a line starting with #+BEGIN_SR and under a line starting with #+END_SR is a code block to be converted into a code cell.

So none of the content starting with #+BEGIN_SR and ending with #+END_SR should be interpreted as a text cell by Pandoc.

Which explanation are you looking for that is not already mentioned in the link from my previous comment?

@tarleb
Copy link
Collaborator

tarleb commented Aug 21, 2022

I found the answer to my questions: it appears that you are using the emacs-ipython package. The use of jupyter-LANG is documented there. This is what I needed to know.

In essence, we should treat src blocks that use a language with a jupyter- prefix as code cells. Conversely, code blocks with class code should probably add this prefix when writing org output.

Side note: The emacs-ipython and ob-ipython packages appear to enjoy a similar level of popularity, but their interfaces are different. It might make sense to support the latter as well.

@tarleb tarleb closed this as completed in 5d66166 Aug 21, 2022
@morgandavidson
Copy link
Author

morgandavidson commented Aug 21, 2022

Glad you found the information you were looking for, I'm sorry I wasn't able to properly understand your question.

Indeed I'm using emacs-jupyter package. Never tried ob-ipython, but it doesn't seem to be maintained (last update was 5 years ago).

@morgandavidson
Copy link
Author

I just tested ipynb export with pandoc 2.19.2 and it works perfectly, bravo! Also I asked emacs-jupyter team to mention exports from org to ipynb through pandoc in their documentation.

@tarleb
Copy link
Collaborator

tarleb commented Aug 25, 2022

Great news, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants