1

I am trying to use ein with org-mode in emacs but I am having problems with exporting the results. Here is a very simple example:

#+BEGIN_SRC ein-python :session localhost :results raw drawer :exports both
  import pandas as pd
  x = pd.DataFrame({'Num': a, 'Text': ['a', 'b', 'c', 'd']})
  x
#+END_SRC

:results:
   Num Text
0    1    a
1    3    b
2    5    c
3   12    d
:end:

So, using :results raw drawer writes the results inside :results: block, and are not shown when I export to html. If instead I use :results output or :results value it simply prints [...] (no data). And :results raw prints the following

#+RESULTS: b2757239-e41b-4626-9aef-d2abf968c1ee
   Num Text
0    1    a
1    3    b
2    5    c
3   12    d

   Num Text
0    1    a
1    3    b
2    5    c
3   12    d

[....]

[....]

Which of course does not look good in the html file. The question, what is the best way to print results to export in html?

0

Probably the simplest way to get reasonable looking results when exporting is to wrap the results in an example block, which will export it as pre-formatted text:

#+BEGIN_SRC python :session localhost :results value :wrap example :exports results
  import pandas as pd
  x = pd.DataFrame({'Num': 3, 'Text': ['a', 'b', 'c', 'd']})
  x
#+END_SRC

#+RESULTS:
#+begin_example
   Num Text
0    3    a
1    3    b
2    3    c
3    3    d
#+end_example

(BTW, I had to change the value of the 'Num' entry in the dataframe to avoid a traceback).

Another possibility is to have the python code produce an HTML table which you then export verbatim. You can have Pandas produce the HTML table for the dataframe by using x.to_html() instead of x in the source block. Then, the trick is to wrap the results appropriately so that the export does not muck around with the HTML that is produced. That is accomplished by wrapping the results in a #+BEGIN_EXPORT html ... #+END_EXPORT block - see the Quoting HTML tags section of the manual. The :wrap header can be used for that as well:

#+BEGIN_SRC python :session localhost :results value :wrap export html :exports both
  import pandas as pd
  x = pd.DataFrame({'Num': 3, 'Text': ['a', 'b', 'c', 'd']})
  x.to_html()
#+END_SRC

#+RESULTS:
#+begin_export html
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Num</th>
      <th>Text</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>3</td>
      <td>a</td>
    </tr>
    <tr>
      <th>1</th>
      <td>3</td>
      <td>b</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>c</td>
    </tr>
    <tr>
      <th>3</th>
      <td>3</td>
      <td>d</td>
    </tr>
  </tbody>
</table>
#+end_export


Exporting this to HTML produces a "real" HTML table.

I would recommend that you avoid raw if possible (and in this case you can): raw does not let Org mode know where the results begin and end, so evaluating the block repeatedly adds more and more crud that you have to clean up manually.

| improve this answer | |
  • Thanks for the advises, it helps me to know better the use of raw. However when I tried your first example, the minibuffer below prints Code block returned no value. I tried also :results raw drawer :wrap example but it again prints :results: and not exporting the block. Any idea why? – Manuel Teodoro May 5 at 14:03
  • Did you try with just plain "python" ? I don't know what ein-python is and I certainly don't have it available, so I can't try with it (I should have mentioned that before). If you get the "no value" response after you cut-and-paste just my first code block into a file and pressing C-c C-c, then all I can think of is a version mismatch (although that's unlikely). Can you add the output of C-u M-x org-version to your question? I can then check whether I get different results with that version. – NickD May 5 at 15:37
  • That's right, I tried your example exactly as it is, using python only and ein-python as well as other options and the result is still no value. I have Org mode version 9.3.6. One more thing, last time it printed a warning: Warning (python): Your ‘python-shell-interpreter’ doesn’t seem to support readline, yet ‘python-shell-completion-native-enable’ was t and "python" is not part of the ‘python-shell-completion-native-disabled-interpreters’ list. Native completions have been disabled locally. Not sure if it has something to do, to me it looks like something else – Manuel Teodoro May 5 at 19:03
  • It's not a version mismatch: I'm also running 9.3.6. The only other thing I can imagine is that your init file is doing something weird. Try starting emacs like this: emacs -q -l ob-python foo.org with foo.org containing just my first code block. Then evaluate it with C-c C-c. It should give the output I'm showing above. If it does, there is probably something wrong with your init file. – NickD May 5 at 19:27
  • Thank you very much, indeed it was the init file. I don't really understood exactly the problem but it is connected to anaconda. Basically when I was trying to use any python interpreter from anaconda, babel was printing no results, even if I had (setq org-babel-python-command "/home/anaconda3/bin/python3"). I've changed it to simply python3 and now is working as you described above. – Manuel Teodoro May 7 at 8:21

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.