Skip to content Skip to sidebar Skip to footer

Where/how Does Jupyter Notebook Hook Into Python's Repr/str

[In jupyter notebook: python3 kernel] If I make a data-frame in pandas or a matrix in sympy it prints very nicely ie it looks quite like a spreadsheet/matrix etc How does it do it?

Solution 1:

I can only speak for pandas but I imagine sympy works in a similar way.

Pandas converts a DataFrame into an HTML string. If we print this string it it looks even worse than a when we just print a DataFrame.

from IPython.display import display, HTML
import pandas as pd

data = pd.DataFrame({'col': range(5)})

print(data._repr_html_())

'<div>\n<style scoped>\n    .dataframe tbody tr....'

If instead we use Jupyter's HTML to display this, Jupyter knows to render this in the web browser as HTML and thus is is displayed as an HTML table.

HTML(data._repr_html_())

    col
0   0
1   1
2   2
3   3
4   4

Post a Comment for "Where/how Does Jupyter Notebook Hook Into Python's Repr/str"