[IPython-dev] How to display markdown text in DataFrame in Notebook

Matthias Bussonnier bussonniermatthias at gmail.com
Mon Jun 1 13:25:52 EDT 2015


Hi, 

> On May 30, 2015, at 13:00, Dong Ta <dongta.hds at gmail.com> wrote:
> 
> Hi Matthias,
> 
> After reading your answer and the notebook, I still can't figure out how to properly display markdown columns in a DataFrame.


You have 2 well, maybe 3 solutions. 

the first 2 require you to write a function that create a markdown repr for panda data frame. 
ie, a function that will return a string like [1]

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell

either you provide a path to panda so that data frames expose that (solution1)
Or you register it as a markdown display formatter, (solution 2)

The third is actually convert the markdown to html yourself, and set the content of the cell you like to the generated HTML.


— 
M

[1]: roughly that:

from IPython.display import Markdown
df = pandas.DataFrame([['a','b','c'],['d','e','f']])

def df_row(row):
    return '|'.join(row[1])
    

def dfm(df):
    return Markdown('\n'.join((
    '|'.join(map(str, df.columns)),
    '|'.join(map(lambda x: '-', df.columns)),
    '\n'.join(map(df_row, df.iterrows()))
     )))
dfm(df)


> 
> Can you be more specific?
> 
> Thanks a lot.
> -DT
> 
> On Sat, May 30, 2015 at 9:48 AM, Matthias Bussonnier <bussonniermatthias at gmail.com <mailto:bussonniermatthias at gmail.com>> wrote:
> 
>> On May 30, 2015, at 03:59, Anton Akhmerov <anton.akhmerov at gmail.com <mailto:anton.akhmerov at gmail.com>> wrote:
>> 
>> Somewhat paradoxically, there's no easy way to display markdown
>> programmatically in ipython. You can manually create HTML out of
>> markdown by using markdown2html filters from
>> IPython.nbconvert.filters.
> 
> 
> 
> For some reason people don’t try, or don’t read the doc:
> 
> class o:
>     def _repr_markdown_(self):
>         return '[try it](https://try.jupyter.org <https://try.jupyter.org/>)'
>     
> o()
> <Screen Shot 2015-05-30 at 09.42.04.png>
> 
> And we support arbitrary mime type, the _repr_*_ that exist are here just for convenience.
> 
>> If you want a more systematic solution, I believe you could subclass
>> the DataFrame 
> 
> Which is the bad solution also, register a display_formatter for a specific object type:
> 
> https://nbviewer.jupyter.org/github/ipython/ipython-in-depth/blob/bigsplit/examples/IPython%20Kernel/Custom%20Display%20Logic.ipynb#Adding-IPython-display-support-to-existing-objects <https://nbviewer.jupyter.org/github/ipython/ipython-in-depth/blob/bigsplit/examples/IPython%20Kernel/Custom%20Display%20Logic.ipynb#Adding-IPython-display-support-to-existing-objects>
> 
>> M
> 
> 
>> 
>> Best,
>> Anton
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org <mailto:IPython-dev at scipy.org>
>> http://mail.scipy.org/mailman/listinfo/ipython-dev <http://mail.scipy.org/mailman/listinfo/ipython-dev>
> 
> 
>> M
> 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org <mailto:IPython-dev at scipy.org>
> http://mail.scipy.org/mailman/listinfo/ipython-dev <http://mail.scipy.org/mailman/listinfo/ipython-dev>
> 
> 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150601/32e104df/attachment.html>


More information about the IPython-dev mailing list