6. Branching with Gate
Route the five-row city report to review if any city is below target. Otherwise mark it ready. The complete script extends the parameterized report.
gate = ff.Gate(report, ff.col("shortfall") > 0)
review = gate.then.with_columns(ff.lit("review").alias("route"))
ready = gate.otherwise.with_columns(ff.lit("ready").alias("route"))
routed = ff.concat([review, ready], how="diagonal_relaxed")
A formula Gate opens its then exit if at least one row matches. It passes the entire input frame through that exit. Here all five cities enter review, including the two above target. otherwise is the else exit; else_ is an alias.
A filter decides which rows continue and group_by() summarizes them; a Gate decides which downstream branch runs, and passes its whole input through that branch.
The inactive branch is skipped. Rejoin the two branches with the native Union form shown above; the default vertical concat produces a different node and is not a substitute for this routing join.
Collection now runs the graph lineage
A frame below a Gate needs a graph run to determine its live exit. Its collect() runs that frame and its ancestors with the current parameter values. It does not run unrelated writers elsewhere in the graph.
Check it
routed.flow_graph.print_tree()
print(routed.collect())
ff.set_flow_parameter(routed, minimum, 1)
print(routed.collect())
The script prints:
● Read data (11) Supermarket invoices
│
● Drop duplicates (12)
│
● Filter data (13)
│
● Formula (14)
├─┐
● │ Filter data (15)
│ ● Filter data (16)
├─┘
● Union data (17)
│ ● Manual input (18)
├─┘
● Join (19)
│
● Filter data (20)
│
● Group by (21)
│
● Formula (23)
│
● Sort data (24)
│
● Gate (25)
├─┐
● │ Formula (27) [then]
│ ● Formula (29) [else]
├─┘
● Union data (30)
shape: (5, 6)
┌───────────┬────────┬──────────┬─────────┬───────────┬────────┐
│ city ┆ orders ┆ revenue ┆ target ┆ shortfall ┆ route │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ u32 ┆ f64 ┆ f64 ┆ f64 ┆ str │
╞═══════════╪════════╪══════════╪═════════╪═══════════╪════════╡
│ Bago ┆ 59 ┆ 28592.7 ┆ 30000.0 ┆ 1407.3 ┆ review │
│ Mandalay ┆ 73 ┆ 36937.24 ┆ 35000.0 ┆ -1937.24 ┆ review │
│ Naypyitaw ┆ 51 ┆ 23733.16 ┆ 25000.0 ┆ 1266.84 ┆ review │
│ Taunggyi ┆ 63 ┆ 32710.95 ┆ 30000.0 ┆ -2710.95 ┆ review │
│ Yangon ┆ 68 ┆ 34096.0 ┆ 35000.0 ┆ 904.0 ┆ review │
└───────────┴────────┴──────────┴─────────┴───────────┴────────┘
shape: (5, 6)
┌───────────┬────────┬──────────┬─────────┬───────────┬───────┐
│ city ┆ orders ┆ revenue ┆ target ┆ shortfall ┆ route │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ u32 ┆ f64 ┆ f64 ┆ f64 ┆ str │
╞═══════════╪════════╪══════════╪═════════╪═══════════╪═══════╡
│ Bago ┆ 204 ┆ 59064.26 ┆ 30000.0 ┆ -29064.26 ┆ ready │
│ Mandalay ┆ 215 ┆ 68589.82 ┆ 35000.0 ┆ -33589.82 ┆ ready │
│ Naypyitaw ┆ 176 ┆ 48556.03 ┆ 25000.0 ┆ -23556.03 ┆ ready │
│ Taunggyi ┆ 199 ┆ 67220.88 ┆ 30000.0 ┆ -37220.88 ┆ ready │
│ Yangon ┆ 206 ┆ 64001.27 ┆ 35000.0 ┆ -29001.27 ┆ ready │
└───────────┴────────┴──────────┴─────────┴───────────┴───────┘
The graph splits at Gate: the first node on each exit carries its name, [then] or [else], and the exits meet again at Union data. With the default minimum of eight, three cities are below target, so the whole report takes the review exit. With a minimum of one, every city is above its target and the report takes the ready exit. The targets stay fixed when the basket threshold changes: this comparison shows the effect of broadening the included sales.
Complete script
Show the complete runnable script
"""Route the whole city report to review or ready with a Gate."""
import flowfile as ff
# Chapters 1-3: read, clean, and enrich the invoices.
SALES = "https://raw.githubusercontent.com/edwardvaneechoud/flowfile/main/data/templates/supermarket_sales.csv"
sales = ff.read_csv(SALES, description="Supermarket invoices")
valid = sales.unique().filter(flowfile_formula="[quantity] > 0 and [unit_price] > 0")
orders = valid.with_columns(flowfile_formulas=["[unit_price] * [quantity]"], output_column_names=["revenue"])
members = orders.filter(ff.col("customer_type") == "Member")
regulars = orders.filter(ff.col("customer_type") == "Normal")
combined = ff.concat([members, regulars], how="diagonal_relaxed")
targets = ff.from_dict(
{
"city": ["Bago", "Mandalay", "Naypyitaw", "Taunggyi", "Yangon"],
"target": [30000.0, 35000.0, 25000.0, 30000.0, 35000.0],
}
)
enriched = combined.join(targets, on="city", how="left")
# Chapters 4-5: the city report, filtered by the min_quantity parameter.
minimum = ff.add_flow_parameter(enriched, ff.Parameter("min_quantity", default=8, type="integer"))
bulk = enriched.filter(ff.col("quantity") >= minimum)
summary = bulk.group_by("city").agg(
ff.col("invoice_id").count().alias("orders"),
ff.col("revenue").sum().alias("revenue"),
ff.col("target").first().alias("target"),
)
report = summary.with_columns((ff.col("target") - ff.col("revenue")).alias("shortfall")).sort("city")
# Chapter 6: route the whole report.
gate = ff.Gate(report, ff.col("shortfall") > 0)
review = gate.then.with_columns(ff.lit("review").alias("route"))
ready = gate.otherwise.with_columns(ff.lit("ready").alias("route"))
routed = ff.concat([review, ready], how="diagonal_relaxed")
routed.flow_graph.print_tree()
print(routed.collect())
ff.set_flow_parameter(routed, minimum, 1)
print(routed.collect())
Recap
You can route an entire report without filtering away context. The Union keeps the output shape consistent whichever branch runs. Next, write that routed result.
Next: Write results