Scenario to Table
Compute a table from an assembled scenario using Python.
When to use this¶
- Aggregate demand, costs or other scenario tables into report rows
- Compute scalar metrics as a single row
- Prepare scenario data for further row processing or export
Use Scenario Script to modify and return a scenario. Use the core Script node for custom logic on raw rows before a scenario exists.
What you need¶
- A scenario wired into Scenario Data
- Optionally, rows wired into Auxiliary Table
Parameters¶
| Setting | What it is |
|---|---|
| Python code | Required main(scenario_data) function returning a list of row dictionaries. |
The optional second argument is table_data, a list of auxiliary rows or None.
Only the Python standard library allowed by Scenario Script is available; code
runs in the same isolated runner. Scenario Script's reference describes the
scenario tables available to both nodes.
Output¶
A list of row dictionaries, including an empty list, is accepted. A scenario,
metrics dictionary or scalar is refused. For metrics, return [metrics].
Rows can feed row-processing nodes or Save Data. They do not become dashboard
results; use a scenario-producing analysis when the answer needs a dashboard.
Example¶
def main(scenario_data):
totals = {}
for row in scenario_data.get("demand_policy", []):
location = row["location"]
totals[location] = totals.get(location, 0) + row["requested_quantity"]
return [
{"location": location, "total_requested": quantity}
for location, quantity in sorted(totals.items())
]
Workflow wiring¶
flowchart LR
n1["Load Scenario"] --> n2["Scenario to Table (into Scenario Data)"]
n2 --> n3["Save Data"]