Skip to content

Commit

Permalink
example: improve polars example
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick committed Jan 14, 2025
1 parent c6c232a commit 07e8d14
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions examples/third_party/polars/polars_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@

import marimo

__generated_with = "0.8.19"
__generated_with = "0.10.12"
app = marimo.App(width="full")


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(
f"""
# Using `Polars` in `marimo`
"""
# Using `Polars` in `marimo`
> Lightning-fast DataFrame library for Rust and Python
"""
> Lightning-fast DataFrame library for Rust and Python
"""
)
return


@app.cell
def __():
def _():
import marimo as mo
import polars as pl
import numpy as np
Expand All @@ -36,15 +36,15 @@ def __():


@app.cell
def __(pl):
def _(pl):
df = pl.read_csv(
"https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv"
)
return (df,)


@app.cell
def __(df, mo):
@app.cell(hide_code=True)
def _(df, mo):
# get all unique values
values_1 = df["Type 1"].unique().drop_nulls().to_list()
values_2 = df["Type 2"].unique().drop_nulls().to_list()
Expand All @@ -59,12 +59,28 @@ def __(df, mo):
)


mo.hstack([type_1_filter, type_2_filter])
mo.hstack([type_1_filter, type_2_filter], justify="start")
return type_1_filter, type_2_filter, values_1, values_2


@app.cell
def __(alt, filtered, mo):
def _(df, pl, type_1_filter, type_2_filter):
filtered = df
if type_1_filter.value:
filtered = filtered.filter(pl.col("Type 1") == type_1_filter.value)
if type_2_filter.value:
filtered = filtered.filter(pl.col("Type 2") == type_2_filter.value)
return (filtered,)


@app.cell(hide_code=True)
def _(mo):
mo.md(r"""Select points on the chart 👇""")
return


@app.cell(hide_code=True)
def _(alt, filtered, mo):
_chart = (
alt.Chart(filtered)
.mark_circle()
Expand All @@ -77,35 +93,16 @@ def __(alt, filtered, mo):
)
)

mo.ui.altair_chart(_chart, legend_selection=True, label="Attack vs Defense")
return


@app.cell
def __(df, pl, type_1_filter, type_2_filter):
filtered = df
if type_1_filter.value:
filtered = filtered.filter(pl.col("Type 1") == type_1_filter.value)
if type_2_filter.value:
filtered = filtered.filter(pl.col("Type 2") == type_2_filter.value)
return (filtered,)


@app.cell
def __(filtered, mo):
table = mo.ui.table(filtered)
table
return (table,)
chart = mo.ui.altair_chart(
_chart, legend_selection=True, label="Attack vs Defense"
)
chart
return (chart,)


@app.cell
def __(mo, table):
mo.vstack(
[
mo.ui.table(table.value, label="Selected", selection=None),
table.value,
]
)
def _(chart, mo):
mo.ui.table(chart.value)
return


Expand Down

0 comments on commit 07e8d14

Please sign in to comment.