|
| 1 | +# marimo + Ibis |
| 2 | + |
| 3 | +If you don't have data to visualize, you can load an example table: |
| 4 | + |
| 5 | +```{python} |
| 6 | +#| code-fold: true |
| 7 | +import ibis |
| 8 | +import ibis.selectors as s |
| 9 | +
|
| 10 | +ibis.options.interactive = True |
| 11 | +
|
| 12 | +penguins = ibis.examples.penguins.fetch() |
| 13 | +penguins.head(3) |
| 14 | +``` |
| 15 | + |
| 16 | +## Using marimo with Ibis |
| 17 | + |
| 18 | +[marimo](https://docs.marimo.io) is a reactive notebook for Python that |
| 19 | +provides interactive UI elements and dataframe tools. You can use marimo's |
| 20 | +interactive features directly with your Ibis tables. |
| 21 | + |
| 22 | +First, import marimo and create an interactive table: |
| 23 | + |
| 24 | +```python |
| 25 | +import marimo as mo |
| 26 | + |
| 27 | +table = mo.ui.table(penguins, selection="multi") |
| 28 | +table |
| 29 | +``` |
| 30 | + |
| 31 | +You can use the `.value` attribute to get the underlying selection in Python as |
| 32 | +an Ibis table. This value can be used to create visualizations, filters, and |
| 33 | +other interactive elements. |
| 34 | + |
| 35 | +```python |
| 36 | +table.value |
| 37 | +``` |
| 38 | + |
| 39 | +### Interactive filtering |
| 40 | + |
| 41 | +Use marimo's no-code dataframe transformer to interactively filter and |
| 42 | +transform your Ibis data: |
| 43 | + |
| 44 | +```python |
| 45 | +transof = mo.ui.dataframe(penguins) |
| 46 | +transof |
| 47 | +``` |
| 48 | + |
| 49 | +The `.value` attribute of the transformer contains the underlying Ibis table. |
| 50 | + |
| 51 | +### Combining filters with Ibis queries |
| 52 | + |
| 53 | +You can combine marimo's UI elements with Ibis queries to create interactive |
| 54 | +filters: |
| 55 | + |
| 56 | +```python |
| 57 | +# Create a bill length slider |
| 58 | +min = penguins.bill_length_mm.min().execute() |
| 59 | +max = penguins.bill_length_mm.max().execute() |
| 60 | +bill_length_slider = mo.ui.slider( |
| 61 | + start=min, |
| 62 | + stop=max, |
| 63 | + value=max, |
| 64 | + label="Max Bill Length (mm)", |
| 65 | +) |
| 66 | +bill_length_slider |
| 67 | +``` |
| 68 | + |
| 69 | +```python |
| 70 | +# Apply the filters to the Ibis query |
| 71 | +filtered_data = penguins.filter( |
| 72 | + [penguins.bill_length_mm <= bill_length_slider.value] |
| 73 | +) |
| 74 | + |
| 75 | +# Display the filtered results in an interactive table |
| 76 | +mo.ui.table(filtered_data) |
| 77 | +``` |
| 78 | + |
| 79 | +### Layout and presentation |
| 80 | + |
| 81 | +Use marimo's layout tools to organize your visualizations: |
| 82 | + |
| 83 | +```python |
| 84 | +# Create tabs for different views of the data |
| 85 | +mo.ui.tabs({ |
| 86 | + "Raw Data": mo.ui.table(penguins), |
| 87 | + "Summary Statistics": mo.ui.table( |
| 88 | + penguins.group_by("species") |
| 89 | + .agg( |
| 90 | + count=ibis._.count(), |
| 91 | + avg_bill_length=ibis._.bill_length_mm.mean(), |
| 92 | + avg_bill_depth=ibis._.bill_depth_mm.mean(), |
| 93 | + ) |
| 94 | + ), |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +## Examples |
| 99 | + |
| 100 | +For more examples, checkout the Ibis notebooks in the marimo repo: |
| 101 | +<https://github.com/marimo-team/marimo/tree/main/examples/third_party/ibis>. |
0 commit comments