Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: add widths to flex (#205)

Myles Scolnick12cf180b1b25
diff --git a/marimo/_plugins/stateless/flex.py b/marimo/_plugins/stateless/flex.py
index f28cfeba78..c274c19e02 100644
--- a/marimo/_plugins/stateless/flex.py
+++ b/marimo/_plugins/stateless/flex.py
@@ -20,6 +20,7 @@ def _flex(
align: Optional[Literal["start", "end", "center", "stretch"]],
wrap: bool,
gap: float,
+ widths: Optional[Sequence[float]],
) -> Html:
justify_content_map = {
"start": "flex-start",
@@ -46,7 +47,19 @@ def _flex(
"gap": f"{gap}rem",
}
)
- grid_items = [h.div(as_html(item).text) for item in items]
+
+ def create_style_for_item(idx: int) -> str:
+ if widths is None:
+ return ""
+ width = widths[idx]
+ if width is None:
+ return ""
+ return create_style({"flex": f"{width}"})
+
+ grid_items = [
+ h.div(as_html(item).text, style=create_style_for_item(i))
+ for i, item in enumerate(items)
+ ]
return Html(h.div(grid_items, style=style))
@@ -92,6 +105,7 @@ def vstack(
align=align,
wrap=False,
gap=gap,
+ widths=None,
)
@@ -104,6 +118,7 @@ def hstack(
align: Optional[Literal["start", "end", "center", "stretch"]] = None,
wrap: bool = False,
gap: float = 0.5,
+ widths: Optional[Literal["equal"] | Sequence[float]] = None,
) -> Html:
"""Stack items horizontally, in a row.
@@ -132,6 +147,9 @@ def hstack(
- `align`: Align items vertically: start, end, center, or stretch.
- `wrap`: Wrap items or not.
- `gap`: Gap between items as a float in rem. 1rem is 16px by default.
+ - `widths`: "equal" to give items equal width; or a list of relative widths
+ with same length as `items`, eg, [1, 2] means the second item is twice as
+ wide as the first; or `None` for a sensible default
**Returns.**
@@ -144,6 +162,7 @@ def hstack(
align=align,
wrap=wrap,
gap=gap,
+ widths=[1 for _ in range(len(items))] if widths == "equal" else widths,
)
diff --git a/marimo/_plugins/stateless/test_flex.py b/marimo/_plugins/stateless/test_flex.py
index d8d89d8513..cdbe409964 100644
--- a/marimo/_plugins/stateless/test_flex.py
+++ b/marimo/_plugins/stateless/test_flex.py
@@ -16,3 +16,9 @@ def test_hstack() -> None:
result.text
== "<div style='display: flex;flex-direction: row;justify-content: center;align-items: normal;flex-wrap: wrap;gap: 1rem'><div><span>item1</span></div><div><span>item2</span></div></div>" # noqa: E501
)
+
+ result = hstack(["item1", "item2"], align="center", widths=[1, 2])
+ assert (
+ result.text
+ == "<div style='display: flex;flex-direction: row;justify-content: space-between;align-items: center;flex-wrap: nowrap;gap: 0.5rem'><div style='flex: 1'><span>item1</span></div><div style='flex: 2'><span>item2</span></div></div>" # noqa: E501
+ )
diff --git a/marimo/_smoke_tests/charts/1mil_flights.py b/marimo/_smoke_tests/charts/1mil_flights.py
index b78d952aca..6fb840e829 100644
--- a/marimo/_smoke_tests/charts/1mil_flights.py
+++ b/marimo/_smoke_tests/charts/1mil_flights.py
@@ -1,7 +1,7 @@
# Copyright 2023 Marimo. All rights reserved.
import marimo
-__generated_with = "0.1.24"
+__generated_with = "0.1.25"
app = marimo.App(width="full")
@@ -11,24 +11,24 @@ def __(pd):
all_flights = pd.read_parquet(
"https://vegafusion-datasets.s3.amazonaws.com/vega/flights_1m.parquet"
)
- return (all_flights,)
+ return all_flights,
@app.cell
def __(mo):
- size = mo.ui.radio(
+ size = mo.ui.dropdown(
label="Size",
options=["100", "1000", "10000", "100000", "1000000"],
value="100000",
)
size
- return (size,)
+ return size,
@app.cell
def __(all_flights, size):
flights = all_flights.sample(int(size.value))
- return (flights,)
+ return flights,
@app.cell
@@ -61,7 +61,7 @@ def __(alt, flights, mo):
alt.Chart(flights).mark_bar().encode(alt.X("delay"), alt.Y("count()"))
)
flight_histogram
- return (flight_histogram,)
+ return flight_histogram,
@app.cell
@@ -191,7 +191,7 @@ def __(alt, brush, flights, mo):
.add_params(brush)
)
mo.output.append(mo.ui.altair_chart(million_histogram))
- return (million_histogram,)
+ return million_histogram,
@app.cell