Spaces:
Running
Running
File size: 5,069 Bytes
15ebd87 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# /// script
# dependencies = [
# "marimo",
# "polars==1.28.1",
# "requests==2.32.3",
# ]
# [tool.marimo.runtime]
# auto_instantiate = false
# ///
import marimo
__generated_with = "0.13.2"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
import polars as pl
import requests
import json
return mo, pl, requests
@app.cell
def _(requests):
json_data = requests.get(
"https://raw.githubusercontent.com/jesshart/fake-datasets/refs/heads/main/orders.json"
)
return (json_data,)
@app.cell
def _(mo):
mo.md(
r"""
# Loading Data
Let's start by loading our data and getting into the `.lazy()` format so our transformations and queries are speedy.
Read more about `.lazy()` here: https://docs.pola.rs/user-guide/lazy/
"""
)
return
@app.cell
def _(json_data, pl):
demand: pl.LazyFrame = pl.read_json(json_data.content).lazy()
demand
return (demand,)
@app.cell
def _(mo):
mo.md(
r"""
Above, you will notice that when you reference the object as a standalone, you get out-of-the-box convenince from `marimo`. You have the `Table` and `Query Plan` options to choose from.
- 💡 Try out the `Table` view! You can click the `Preview data` button to get a quick view of your data.
- 💡 Take a look at the `Query plan`. Learn more about Polar's query plan here: https://docs.pola.rs/user-guide/lazy/query-plan/
"""
)
return
@app.cell
def _(mo):
mo.md(
r"""
# marimo's Native Dataframe UI
There are a few ways to leverage marimo's native dataframe UI. One is by doing what we saw above—by referencing a `pl.LazyFrame` directly. You can also try,
- Reference a `pl.LazyFrame` (we already did this!)
- Referencing a `pl.DataFrame` and see how it different from its corresponding lazy version
- Use `mo.ui.table`
- Use `mo.ui.dataframe`
"""
)
return
@app.cell
def _(mo):
mo.md(
r"""
## Reference a pl.DataFrame
Let's reference the same frame as before, but this time as a `pl.DataFrame` by calling `.collect()` on it.
"""
)
return
@app.cell
def _(demand):
demand.collect()
return
@app.cell
def _(mo):
mo.md(
r"""
Note how much functionality we have right out-of-the-box. Click on column names to see rich features like sorting, freezing, filtering, searching, and more!
Notice how `order_quantity` has a green bar chart under it indicating the ditribution of values for the field!
Don't miss the `Download` feature as well which supports downloading in CSV, json, or parquet format!
"""
)
return
@app.cell
def _(mo):
mo.md(
r"""
## Use `mo.ui.table`
The `mo.ui.table` allows you to select rows for use downstream. You can select the rows you want, and then use these as filtered rows downstream.
"""
)
return
@app.cell
def _(demand, mo):
demand_table = mo.ui.table(demand, label="Demand Table")
return (demand_table,)
@app.cell
def _(demand_table):
demand_table
return
@app.cell
def _(mo):
mo.md(r"""I like to use this feature to select groupings based on summary statistics so I can quickly explore subsets of categories. Let me show you what I mean.""")
return
@app.cell
def _(demand, pl):
summary: pl.LazyFrame = demand.group_by("product_family").agg(
pl.mean("order_quantity").alias("mean"),
pl.sum("order_quantity").alias("sum"),
pl.std("order_quantity").alias("std"),
pl.min("order_quantity").alias("min"),
pl.max("order_quantity").alias("max"),
pl.col("order_quantity").null_count().alias("null_count"),
)
return (summary,)
@app.cell
def _(mo, summary):
summary_table = mo.ui.table(summary)
return (summary_table,)
@app.cell
def _(summary_table):
summary_table
return
@app.cell
def _(mo):
mo.md(
r"""
Now, instead of manually creatinga filter for what I want to take a closer look at, I simply select from the ui and do a simple join to get that aggregated level with more detail.
The following cell uses the output of the `mo.ui.table` selection, selects its unique keys, and uses that to join for the selected subset of the original table.
"""
)
return
@app.cell
def _(demand, pl, summary_table):
selection_keys: pl.LazyFrame = (
summary_table.value.lazy().select("product_family").unique()
)
selection: pl.lazyframe = selection_keys.join(
demand, on="product_family", how="left"
)
selection.collect()
return
@app.cell
def _(mo):
mo.md(r"""## Use `mo.ui.dataframe`""")
return
@app.cell
def _(demand, mo):
mo_dateframe = mo.ui.dataframe(demand.collect())
return (mo_dateframe,)
@app.cell
def _(mo_dateframe):
mo_dateframe
return
@app.cell
def _():
return
if __name__ == "__main__":
app.run()
|