优化org src codes输出results的formatting

Upon learning pandas on org mode of emacs, tried the following codes:

scientists = pd.DataFrame(
data={'Occupation': ['Chemist', 'Statistician'],
'Born': ['1920-07-25', '1876-06-13'],
'Died': ['1958-04-16', '1937-10-16'],
'Age': [37, 61]},
index=['Rosaline Franklin', 'William Gosset'],
columns=['Occupation', 'Born', 'Died', 'Age'])
print(scientists)

Jupyter produced

Unfortunately, the org’s babel does not produce very pretty format

Screenshot%20from%202019-06-20%2015-54-17

The header was not placed in correct column.

How could improve the format ?

ob-ipython will display anything back from ipython with the mime type ‘text/org’ verbatim. This allows you and others to create formatters that output raw org. For example, drop this in your ipython startup file to have arrays and dataframes rendered as org tables:

import IPython
from tabulate import tabulate

class OrgFormatter(IPython.core.formatters.BaseFormatter):
    def __call__(self, obj):
        try:
            return tabulate(obj, headers='keys',
                            tablefmt='orgtbl', showindex='always')
        except:
            return None

ip = get_ipython()
ip.display_formatter.formatters['text/org'] = OrgFormatter()

这是个bug。我已经提过issue了,也有修正

我已经放弃ob-ipython改用emacs-jupyter了,整体体验强百倍

1 个赞