This is a follow-up to my previous post .
marimo
The widgets may take a few moments to load, as they rely on WebAssembly under the hood.
Here, I demonstrate how marimo widgets can be embedded in Great Tables by wrapping them with the html()
function provided by the library. This allows interactive widgets to control the table’s appearance in a Quarto environment via WASM — a surprisingly powerful capability, in my opinion.
import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20html
data%20%3D%20%7B%0A%20%20%20%20%22col1%22%3A%20%5B2%2C%205%2C%207%2C%2010%2C%2015%5D%2C%0A%20%20%20%20%22col2%22%3A%20%5B%22x%22%2C%20%22y%22%2C%20%22y%22%2C%20%22z%22%2C%20%22z%22%5D%2C%0A%20%20%20%20%22color%22%3A%20%5B%22lightgrey%22%2C%20%22lightblue%22%2C%20%22lightblue%22%2C%20%22papayawhip%22%2C%20%22papayawhip%22%5D%2C%0A%7D%0Adf%20%3D%20pl.DataFrame(data)%0Aprint(df)
style_widget%20%3D%20mo.ui.slider(1%2C%206%2C%20label%3D%22Style%20Number%22)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%20value%3D_colors%5B0%5D%2C%20label%3D%22Style%20Color%22%2C%20inline%3DTrue%0A)%0A%0Arow_striping_widget%20%3D%20mo.ui.switch(value%3DTrue%2C%20label%3D%22Row%20Striping%3F%22)%0A%0Agt%20%3D%20(%0A%20%20%20%20GT(df)%0A%20%20%20%20.tab_header(html(style_widget)%2C%20html(color_widget))%0A%20%20%20%20.tab_source_note(html(row_striping_widget))%0A%20%20%20%20.opt_align_table_header(%22left%22)%0A)
gt.opt_stylize(%0A%20%20%20%20style%3Dstyle_widget.value%2C%0A%20%20%20%20color%3Dcolor_widget.value%2C%0A%20%20%20%20add_row_striping%3Drow_striping_widget.value%2C%0A)
Check out the full marimo code below.
Show full code
import marimo
__generated_with = "0.13.15"
app = marimo.App(width= "medium" )
@app.cell
def _():
import marimo as mo
import polars as pl
from great_tables import GT, html
return GT, html, mo, pl
@app.cell
def _(pl):
data = {
"col1" : [2 , 5 , 7 , 10 , 15 ],
"col2" : ["x" , "y" , "y" , "z" , "z" ],
"color" : ["lightgrey" , "lightblue" , "lightblue" , "papayawhip" , "papayawhip" ],
}
df = pl.DataFrame(data)
return (df,)
@app.cell
def _(GT, df, html, mo):
style_widget = mo.ui.slider(1 , 6 , label= "Style Number" )
_colors = ["blue" , "cyan" , "pink" , "green" , "red" , "gray" ]
color_widget = mo.ui.radio(
options= _colors, value= _colors[0 ], label= "Style Color" , inline= True
)
row_striping_widget = mo.ui.switch(value= True , label= "Row Striping?" )
gt = (
GT(df)
.tab_header(html(style_widget), html(color_widget))
.tab_source_note(html(row_striping_widget))
.opt_align_table_header("left" )
)
return color_widget, gt, row_striping_widget, style_widget
@app.cell
def _(color_widget, gt, row_striping_widget, style_widget):
gt.opt_stylize(
style= style_widget.value,
color= color_widget.value,
add_row_striping= row_striping_widget.value,
)
return
if __name__ == "__main__" :
app.run()
This post was drafted by me, with AI assistance to refine the content.