Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions examples/saved_scenario_create_update.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Saving Scenarios to MyETM\n",
"\n",
"This notebook demonstrates how to save scenarios to MyETM as SavedScenarios."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from example_helpers import setup_notebook\n",
"setup_notebook()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pyetm.clients.base_client import BaseClient\n",
"from pyetm.models.scenario import Scenario\n",
"from pyetm.models.saved_scenario import SavedScenario\n",
"\n",
"client = BaseClient()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using scenario.save()\n",
"\n",
"The simplest way to save a scenario is using the `.save()` method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a session scenario\n",
"scenario = Scenario.new(area_code=\"nl\", end_year=2050, title=\"My Test Scenario\")\n",
"print(f\"Created session scenario {scenario.id}\")\n",
"\n",
"# Save it to MyETM - automatically uses scenario.id, scenario.title, and scenario.private\n",
"saved_scenario = scenario.save(description=\"Saved using save()\")\n",
"\n",
"print(f\"Saved as SavedScenario {saved_scenario.id}\")\n",
"print(f\"Title: {saved_scenario.title}\")\n",
"print(f\"Description: {saved_scenario.description}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The underlying scenario's title will be taken for the saved scenario, unless another title is specified. If there are no titles, an error will be thrown."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenario2 = Scenario.new(area_code=\"nl\", end_year=2050, title=\"Original Title\")\n",
"saved_scenario2 = scenario2.save(title=\"Custom Title\", private=True)\n",
"\n",
"print(f\"Scenario title: {scenario2.title}\")\n",
"print(f\"SavedScenario title: {saved_scenario2.title}\")\n",
"print(f\"Private: {saved_scenario2.private}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Accessing the underlying scenario\n",
"\n",
"You can update the title, description, and privacy settings:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"underlying_scenario = saved_scenario.get_scenario(client)\n",
"print(f\"Scenario ID: {underlying_scenario.id}\")\n",
"print(f\"Area code: {underlying_scenario.area_code}\")\n",
"print(f\"End year: {underlying_scenario.end_year}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Alternative Methods to create a saved scenario\n",
"\n",
"You can also use `SavedScenario.create()` or `SavedScenario.from_scenario()` directly:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Method 2: Using SavedScenario.from_scenario()\n",
"scenario3 = Scenario.new(area_code=\"nl\", end_year=2050)\n",
"saved3 = SavedScenario.from_scenario(client, scenario3, \"From scenario method\")\n",
"print(f\"Method 2 - SavedScenario ID: {saved3.id}\")\n",
"\n",
"# Method 3: Using SavedScenario.create() with explicit params\n",
"saved4 = SavedScenario.create(\n",
" client,\n",
" params={\n",
" \"scenario_id\": scenario3.id,\n",
" \"title\": \"Created with explicit params\",\n",
" \"description\": \"Using SavedScenario.create()\",\n",
" \"private\": False\n",
" }\n",
")\n",
"print(f\"Method 3 - SavedScenario ID: {saved4.id}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Update\n",
"\n",
"You can update any of the saved scenario's modifiable fields such as title and description."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"saved_scenario.update(\n",
" client,\n",
" title=\"Updated Title\",\n",
" description=\"Updated description\"\n",
")\n",
"\n",
"print(f\"Updated title: {saved_scenario.title}\")\n",
"print(f\"Updated description: {saved_scenario.description}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pyetm-Rh4Np-o3-py3.12",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion src/pyetm/clients/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def content(self) -> bytes:

@field_validator("status_code", mode="before")
@classmethod
def raise_for_status(cls, value, info: ValidationInfo) -> int:
def raise_for_status(cls, value, info: ValidationInfo) -> None:
"""Raise appropriate exception for HTTP errors."""
if value == 401:
raise PermissionError("Invalid or missing ETM_API_TOKEN")
Expand Down
1 change: 1 addition & 0 deletions src/pyetm/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .custom_curves import CustomCurves
from .gqueries import Gqueries
from .inputs import Input, Inputs
from .saved_scenario import SavedScenario
from .scenario import Scenario
from .scenarios import Scenarios
from .sortables import Sortable, Sortables
Expand Down
Loading