from reactable import Reactable, embed_css, to_widget
from reactable.data import sleep
Displaying tables
reactable tables can be displayed in a number of environments, include Jupyter notebooks, VS Code, and quarto documents (.qmd). Moreover, the package htmltools can be used to customize HTML in and around the table.
For the examples on this page, we’ll use some commen reactable functions, and the sleep dataset.
Basics
In order to display a table, be sure to call embed_css()
once per notebook or qmd file.
embed_css()
3]) Reactable(sleep[:
Embedding css adds the necessary CSS styles to the document, so that the table is laid out correctly.
Custom CSS styles
In order to apply custom CSS styles, they need to be added to the notebook or qmd file. One way to do this is using the display()
function from IPython.
from IPython.display import display, HTML
display(
HTML("""
<style>
.sleep-table {
font-family: Courier New, sans-serif;
}
</style>
"""
)
)
3], class_="sleep-table") Reactable(sleep[:
Using with htmltools
Oftentimes, it’s useful to wrap tables in additional HTML elements, like a div with a title in it. This can be done using the htmltools package.
The example below shows adding an <h3>
title to a table.
import htmltools as ht
= ht.div(
tag "Sleep data", style=ht.css(color="red")),
ht.h3(3]),
Reactable(sleep[:
)
to_widget(tag)
Notice two important pieces:
h3()
allows setting the html style attribute using thestyle=
argument.to_widget()
is necessary to convert htmltools objects with reactable tables inside them, into a widget that can be displayed in the notebook.
See “Using reactable with htmltools” for more examples.
Using with ipyreact
reactable uses ipyreact under the hood to render tables. This means that you can use ipyreact.Widget
to create additional HTML elements and react components around the table.
import anywidget
import ipyreact
ipyreact.Widget(="div",
_type=[
children="h3", children="Sleep data"),
ipyreact.Widget(_type3]).to_widget(),
Reactable(sleep[:
], )