Suppose I have the following org file (pdguide.org):

* Creating a Pandas =DataFrame=
  The pandas =DataFrame= can be created by loading the data from the external, existing storage like a database, SQL or CSV files. But the pandas =DataFrame= can also be created from the lists, dictionary, etc. One of the ways to create a pandas data frame is shown below:
  - import pandas library
    #+begin_src python
    import pandas as pd
  - Dictionary of key pair values called data
    #+begin_src python
    data = {'Name':['NickD', 'John', 'Sam', 'crocefisso', 'Debbie'],
         'Age': [24, 23, 22, 19, 10]}
  - Calling the pandas DataFrame method by passing the dictionary (data) as a parameter
    #+begin_src python
    df = pd.DataFrame(data)

How can I export it to a pdguide.py so the parts that are not code blocks become comments?

Also would it be possible to have it exported as pdguide.ipynb so the part that are not code blocks become markdown?

  • 1
    Please don't use images. Org mode is plain text, so you can cut and paste it into a markdown code block
    – NickD
    Nov 17 '20 at 18:48
  • I already erased the org I wrote for the screenshot. I have tried to edit it as code, but it didn't work since it was transformed in md.
    – crocefisso
    Nov 17 '20 at 18:52
  • See the link and my answer for how to do it: all you have to do is surround it with two lines, each containing three backticks, one before the code and one after.
    – NickD
    Nov 17 '20 at 18:55
  • I added a small section of the text from your image as a markdown code block to illustrate the method. Hope it helps. Fee free to delete it (although I hope you will instead expand it to contain all the text in your image and get rid of the image instead ;-) ).
    – NickD
    Nov 18 '20 at 3:59

The process is called tangling.

All you have to do is provide a header arg to specify that, e.g.:

#+PROPERTY: header-args:python :tangle pdguide.py

After you add the line to your buffer, don't forget to refresh it by typing C-c C-c on the property line.

Then you can tangle the code into the file with C-c C-v C-t.

EDIT: you can include headlines as comments (but not the whole text) by using :comments both as a header arg: that will include the headline as a comment and will provide links back to the Org mode file for each code block. You can use :comments org for just the headline, or :comment link for just the links.

As for your last question: "Also would it be possible to have it exported as pdguide.ipynb so the part that are not code blocks become markdown?", AFAIK the answer is no. EDIT: The OP points out in a comment that something like this might be possible using John Kitchin's 'ipynb' exporter.

  • When I tangle code, a .py is generated but without any comments.
    – crocefisso
    Nov 17 '20 at 19:02
  • As I said I wanted all the none code blocks to be exported as comments. In Python comments begin with a hash mark ( # ).
    – crocefisso
    Nov 17 '20 at 19:21
  • Edited the answer with some info: a small step towards what you want but certainly not the whole thing.
    – NickD
    Nov 17 '20 at 19:30
  • Thx, :comments org works than :comments both though. As for ipynb export, I found this module github.com/jkitchin/ox-ipynb but didn't test it yet.
    – crocefisso
    Nov 17 '20 at 19:59
  • So here is the full line #+PROPERTY: header-args :python :comments org :tangle filename.py
    – crocefisso
    Nov 17 '20 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.