jkitchin / ox-ipynb

org-mode exporter to Jupyter notebooks
170 stars 39 forks source link

:noweb yes is not respected #26

Open methuselah-0 opened 4 years ago

methuselah-0 commented 4 years ago

For example:

+BEGIN_SRC python :tangle picture_output.py :noweb-ref "" :noweb yes

  # export
  def main():
      #<<picture_output.main>>#
  if __name__ == "__main__":
      main()

+END_SRC

is not exported with the #<># noweb-reference expanded. The cell in the resulting ipynb file looks like:

{
  "cell_type": "code",
  "execution_count": 1,
  "metadata": {},
  "outputs": [],
  "source": [
    "# export\ndef main():\n    #<<picture_output.main>>#\nif __name__ == \"__main__\":\n    main()"
  ]
},
jkitchin commented 4 years ago

ox-ipynb is not a regular kind of exporter, so I am not sure this is possible to support. you might have to first export your org file to an org-file, which might work with noweb, and then export that org-file to a notebook.

methuselah-0 commented 4 years ago

Ok, thanks for the tip. I did that and it worked. I wrote a bash-script for it that runs these with emacs --batch mode, but it's too long to post here. This method (with an intermediate org-export) doesn't perfectly equal a proper noweb expansion with links between source-blocks etc, but its better than not using noweb at all.

jkitchin commented 4 years ago

could you send me a small example file that illustrates the issues?

methuselah-0 commented 4 years ago

could you send me a small example file that illustrates the issues?

I don't think I know what else would help to illustrate than the example I already gave, but if lucky, this :noweb example helps:

reference


In the most simple case, the contents of a single source block is inserted within other blocks. Thus, in following example,

+NAME: initialization

+BEGIN_SRC emacs-lisp

(setq sentence "Never a foot too far, even.")

+END_SRC

+BEGIN_SRC emacs-lisp :noweb yes

  <<initialization>>
  (reverse sentence)

+END_SRC

the second code block is expanded as [upon export]:

+BEGIN_SRC emacs-lisp :noweb yes

(setq sentence "Never a foot too far, even.") (reverse sentence)

+END_SRC


So the second code block is what I would expect to see in the ipynb file - not with the <<noweb-reference-name>> preserved.

I am using ox-ipynb to convert from org-babel format -> ipynb format -> then nbdev to produce jekyll-website documentation. Currently, I am expanding the noweb-refs in the org-file, to another org-file by using the org-exporter, and then to ipynb since the :noweb yes and :noweb are not interpreted by the ox-ipynb exporter directly.

See this example(compare the org and exported ipynb files): https://github.com/methuselah-0/nbdev-org-babel-example/blob/master/nbs/ob_example/testscript.org https://github.com/methuselah-0/nbdev-org-babel-example/blob/master/nbs/ob_example/testscript_temp.ipynb

jkitchin commented 4 years ago

I have not forgotten about this. I don't currently have time to fix it though. I am leaving it open, because I hope I will get time later this year.