Application Module¶
This section documents the application components of the Nextmv Python SDK - Local experience.
application
¶
Application module for interacting with local Nextmv applications.
This module provides functionality to interact with applications in Nextmv, including application management, running applications, and managing inputs.
| CLASS | DESCRIPTION |
|---|---|
Application |
Class for interacting with local Nextmv applications. |
Application
¶
Bases: BaseModel
A decision model that can be executed.
You can import the Application class directly from local:
This class represents an application in Nextmv, providing methods to interact with the application, run it with different inputs, manage versions, instances, experiments, and more.
| PARAMETER | DESCRIPTION |
|---|---|
|
Source of the application, when initialized locally. An application's
source typically refers to the directory containing the
TYPE:
|
|
Description of the application.
TYPE:
|
Examples:
>>> from nextmv.local import Application
>>> app = Application(src="path/to/app")
>>> # Retrieve an app's run result
>>> result = app.run_result("run-id")
-
nextmv
💻 Local
Reference
Application Module
applicationApplication
app_id
class-attribute
instance-attribute
¶
ID of the application. This ID is auto-generated if not provided.
content_format
class-attribute
instance-attribute
¶
Warning
InputFormat is deprecated, but kept for backward compatibility. Use
ContentFormat instead.
The content format of the application, which is determined by the manifest.
description
class-attribute
instance-attribute
¶
Description of the application.
from_registry
classmethod
¶
from_registry(
src: str | None = None, app_id: str | None = None
) -> Application
Load an application from the local registry.
The local registry is a YAML file stored at $HOME/.nextmv/registry.yaml
that keeps track of locally registered applications. This method looks
for an entry in the local registry that matches the provided src or
app_id, and loads the application from the corresponding source path.
Specify either the src or the app_id to identify the application to
load. If both are provided, the method will prioritize loading by
app_id.
| PARAMETER | DESCRIPTION |
|---|---|
|
Source path of the application to load. This is typically the path
to the directory containing the application's manifest (
TYPE:
|
|
ID of the application to load. This ID is auto-generated if not provided during registration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Application
|
The loaded application instance. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If neither |
Examples:
Assuming an application is registered in the local registry with the following entry:
>>> from nextmv.local import Application
>>> app = Application.from_registry(app_id="app-123")
>>> print(app.src)
/path/to/app
Source code in nextmv/nextmv/local/application.py
get_or_register
classmethod
¶
get_or_register(
app_src: str, app_id: str | None = None
) -> tuple[Application, bool]
Get a local application from the registry or register it if it does not exist.
This function checks if a local application with the given source and
ID is already registered in the local registry. If it is, it retrieves
and returns that application. If it is not registered, it creates a new
Application instance with the provided source and ID, registers it in
the local registry, and then returns it. The boolean in the returned
tuple indicates whether a new application was registered (True) or an
existing one was retrieved (False).
| PARAMETER | DESCRIPTION |
|---|---|
|
The source path of the application to get or register.
TYPE:
|
|
The ID of the application to get or register. If None, the ID will be derived from the application source.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Application, bool]
|
A tuple containing the local application retrieved from the registry or newly registered, and a boolean indicating whether the application was newly registered. |
Source code in nextmv/nextmv/local/application.py
initialize
classmethod
¶
initialize(
src: str | None = None,
description: str | None = None,
destination: str | None = None,
manifest_type: ManifestType = PYTHON,
content_format: ContentFormat = JSON,
example: str | None = "hello-world",
should_register: bool = False,
) -> Application
Initialize a sample Nextmv application, locally.
This method will create a new application from a template in the local
file system. The application is a dir with the name given by src (it
becomes the source of the app), under the location given by
destination. If the destination parameter is not specified, the
current working directory is used as default. This method will scaffold
the application with the necessary files and directories to have an
opinionated structure for your decision model. The template used is
determined by the combination of manifest_type and content_format.
The example parameter further specifies which example within the
template to use when scaffolding the application. Once the application
is initialized, you are encouraged to complete it with the decision
model itself, so that the application can be run locally.
If the src parameter is not provided, a random name will be generated
for the application. If should_register is set to True, the
application will also be registered in the local registry after
initialization.
| PARAMETER | DESCRIPTION |
|---|---|
|
Source (ID, name) of the application. Will be generated if not provided.
TYPE:
|
|
Description of the application.
TYPE:
|
|
Destination directory where the application will be initialized. If not provided, the current working directory will be used.
TYPE:
|
|
Type of manifest to use for the application. Determines the language/runtime template that will be scaffolded.
TYPE:
|
|
Content format of the application's input and output. Determines which template variant is used when scaffolding the application.
TYPE:
|
|
The example template to use when initializing the application.
Valid values depend on the
TYPE:
|
|
Whether to register the application in the local registry after
initialization. If True, the application will be added to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Application
|
The initialized application instance. |
Source code in nextmv/nextmv/local/application.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
is_registered
¶
Check if the local application is registered in the local registry.
This method checks if the application has an entry in the local registry.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the application is registered, False otherwise. |
Source code in nextmv/nextmv/local/application.py
list_runs
¶
list_runs(status: StatusV2 | None = None) -> list[Run]
List all runs for the application.
| PARAMETER | DESCRIPTION |
|---|---|
|
If specified, only runs with the given status will be returned.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Run]
|
A list of all runs associated with the application. |
Source code in nextmv/nextmv/local/application.py
manifest
class-attribute
instance-attribute
¶
Manifest of the application. A manifest is a file named app.yaml that
must be present at the root of the application's src directory. If you
specify this argument when creating the Application, the provided manifest
will be written to the src directory, overriding any existing manifest
file.
model_post_init
¶
Actions taken after the Application model is initialized.
Source code in nextmv/nextmv/local/application.py
new_run
¶
new_run(
input: Input | dict[str, Any] | BaseModel | str = None,
name: str | None = None,
description: str | None = None,
options: Options | dict[str, str] | None = None,
configuration: RunConfiguration
| dict[str, Any]
| None = None,
json_configurations: dict[str, Any] | None = None,
input_dir_path: str | None = None,
) -> str
Run the application locally with the provided input.
This method is the local equivalent to cloud.Application.new_run,
which submits the input to Nextmv Cloud. This method runs the
application locally using the src of the app.
Make sure that the src attribute is set on the Application class
before running locally, as it is required by the method.
| PARAMETER | DESCRIPTION |
|---|---|
|
Input to use for the run. This can be a If If you want to work with When
When working with JSON data, use the
TYPE:
|
|
Name of the local run.
TYPE:
|
|
Description of the local run.
TYPE:
|
|
Options to use for the run. This can be a
TYPE:
|
|
Configuration to use for the run. This can be a
TYPE:
|
|
Optional configurations for JSON serialization. This is used to customize the serialization before data is sent.
TYPE:
|
|
Path to a directory containing input files. This is useful for
input formats like
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
ID ( |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
FileNotFoundError
|
If the app.yaml file cannot be found in the specified |
Examples:
>>> from nextmv.local import Application
>>> app = Application(id="my-app", src="/path/to/app")
>>> run_id = app.new_run(
... input={"vehicles": [{"id": "v1"}]},
... options={"duration": "10s"}
... )
>>> print(f"Local run completed with ID: {run_id}")
Source code in nextmv/nextmv/local/application.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
new_run_with_result
¶
new_run_with_result(
input: Input | dict[str, Any] | BaseModel | str = None,
name: str | None = None,
description: str | None = None,
run_options: Options | dict[str, str] | None = None,
polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
configuration: RunConfiguration
| dict[str, Any]
| None = None,
json_configurations: dict[str, Any] | None = None,
input_dir_path: str | None = None,
output_dir_path: str | None = ".",
) -> RunResult
Submit an input to start a new local run of the application and poll
for the result. This is a convenience method that combines the
new_run and run_result_with_polling methods, applying
polling logic to check when the local run succeeded.
This method is the local equivalent to
cloud.Application.new_run_with_result, which submits the input to
Nextmv Cloud. This method runs the application locally using the src
of the app.
Make sure that the src attribute is set on the Application class
before running locally, as it is required by the method.
| PARAMETER | DESCRIPTION |
|---|---|
|
Input to use for the run. This can be a If If you want to work with When
When working with JSON data, use the
TYPE:
|
|
Name of the local run.
TYPE:
|
|
Description of the local run.
TYPE:
|
|
Options to use for the run. This can be a
TYPE:
|
|
Options to use when polling for the run result.
TYPE:
|
|
Configuration to use for the run. This can be a
TYPE:
|
|
Optional configurations for JSON serialization. This is used to customize the serialization before data is sent.
TYPE:
|
|
Path to a directory containing input files. This is useful for
input formats like
TYPE:
|
|
Path to a directory where non-JSON output files will be saved. This is required if the output is non-JSON. If the directory does not exist, it will be created. Uses the current directory by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RunResult
|
Result of the run, including output. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
FileNotFoundError
|
If the app.yaml file cannot be found in the specified |
Examples:
>>> from nextmv.local import Application
>>> app = Application(id="my-app", src="/path/to/app")
>>> run_result = app.new_run_with_result(
... input={"vehicles": [{"id": "v1"}]},
... options={"duration": "10s"}
... )
>>> print(f"Local run completed with ID: {run_result.id}")
Source code in nextmv/nextmv/local/application.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |
register
¶
Register the local application in the local registry.
This method adds an entry for the application in the local registry,
which is stored as a YAML file at $HOME/.nextmv/registry.yaml. The
registry keeps track of locally registered applications, allowing you
to manage and list them easily.
If the application is already registered, this method does nothing.
Source code in nextmv/nextmv/local/application.py
run_input
¶
run_input(
run_id: str, output_dir_path: str | None = "."
) -> dict[str, Any] | str | None
Get the input of a local run.
This method retrieves the input of a run that was executed locally
using the new_run or new_run_with_result method. This is the local
equivalent to cloud.Application.run_input, which retrieves the input
of a remote run in Nextmv Cloud.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the run to retrieve input for.
TYPE:
|
|
Path to a directory where non-JSON input files will be saved. This is required if the input is non-JSON. If the directory does not exist, it will be created. Uses the current directory by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | str | None
|
The input of the run depending on the input format. If the input
format is JSON, a dict is returned. If the input format is text, a
string is returned. For multi-file and csv-archive, the input files
are saved to the given |
Source code in nextmv/nextmv/local/application.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 | |
run_logs
¶
run_logs(run_id: str) -> str
Get the logs of a local run.
If the run does not have any logs, or they are empty, then this method
simply returns a blank string. This method is equivalent to fetching
the content of the .nextmv/runs/{run_id}/logs/logs.log file.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the run to retrieve logs for.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The contents of the logs file for the run. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Source code in nextmv/nextmv/local/application.py
run_logs_with_polling
¶
run_logs_with_polling(
run_id: str,
verbose: bool = False,
rich_print: bool = False,
polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
log_func: Callable[[str], None] | None = None,
) -> list[str]
Get the logs of a local run with polling.
Retrieves the logs of a run. This method polls for the logs until the
run finishes executing or the polling strategy is exhausted. It is the
"real-time" equivalent of the run_logs method. After the polling is
done, all the logs are returned as a list of strings (one per line).
You can use the verbose parameter to print the logs as they are
obtained during the polling process. You can also use the rich_print
parameter to enable rich printing for better formatting of the logs.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the run to retrieve the logs for.
TYPE:
|
|
Whether to print the logs as they are obtained during the polling process.
TYPE:
|
|
Whether to use rich printing for better formatting of the logs.
TYPE:
|
|
Options to use when polling for the run logs.
TYPE:
|
|
Optional custom logging function callback. This function is invoked
independently of the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of log lines of the run. |
| RAISES | DESCRIPTION |
|---|---|
TimeoutError
|
If the run does not complete after the polling strategy is exhausted based on time duration. |
RuntimeError
|
If the run does not complete after the polling strategy is exhausted based on number of tries. |
Examples:
>>> from nextmv import PollingOptions
>>> polling_opts = PollingOptions(max_tries=50, max_duration=600)
>>> logs = app.run_logs_with_polling("run-123", polling_opts)
>>> for line in logs:
... print(line)
Starting optimization...
Found initial solution
Source code in nextmv/nextmv/local/application.py
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
run_metadata
¶
run_metadata(run_id: str) -> RunInformation
Get the metadata of a local run.
This method is the local equivalent to
cloud.Application.run_metadata, which retrieves the metadata of a
remote run in Nextmv Cloud. This method is used to get the metadata of
a run that was executed locally using the new_run or
new_run_with_result method.
Retrieves information about a run without including the run output. This is useful when you only need the run's status and metadata.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the run to retrieve metadata for.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RunInformation
|
Metadata of the run (run information without output). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Examples:
>>> metadata = app.run_metadata("run-789")
>>> print(metadata.metadata.status_v2)
StatusV2.succeeded
Source code in nextmv/nextmv/local/application.py
run_result
¶
run_result(
run_id: str, output_dir_path: str | None = "."
) -> RunResult
Get the local result of a run.
This method is the local equivalent to cloud.Application.run_result,
which retrieves the result of a remote run in Nextmv Cloud. This method
is used to get the result of a run that was executed locally using the
new_run or new_run_with_result method.
Retrieves the complete result of a run, including the run output.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the run to get results for.
TYPE:
|
|
Path to a directory where non-JSON output files will be saved. This is required if the output is non-JSON. If the directory does not exist, it will be created. Uses the current directory by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RunResult
|
Result of the run, including output. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Examples:
Source code in nextmv/nextmv/local/application.py
run_result_with_polling
¶
run_result_with_polling(
run_id: str,
polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
output_dir_path: str | None = ".",
) -> RunResult
Get the result of a local run with polling.
This method is the local equivalent to
cloud.Application.run_result_with_polling, which retrieves the result
of a remote run in Nextmv Cloud. This method is used to get the result
of a run that was executed locally using the new_run or
new_run_with_result method.
Retrieves the result of a run including the run output. This method polls for the result until the run finishes executing or the polling strategy is exhausted.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the run to retrieve the result for.
TYPE:
|
|
Options to use when polling for the run result.
TYPE:
|
|
Path to a directory where non-JSON output files will be saved. This is required if the output is non-JSON. If the directory does not exist, it will be created. Uses the current directory by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RunResult
|
Complete result of the run including output data. |
| RAISES | DESCRIPTION |
|---|---|
HTTPError
|
If the response status code is not 2xx. |
TimeoutError
|
If the run does not complete after the polling strategy is exhausted based on time duration. |
RuntimeError
|
If the run does not complete after the polling strategy is exhausted based on number of tries. |
Examples:
>>> from nextmv import PollingOptions
>>> # Create custom polling options
>>> polling_opts = PollingOptions(max_tries=50, max_duration=600)
>>> # Get run result with polling
>>> result = app.run_result_with_polling("run-123", polling_opts)
>>> print(result.output)
{'solution': {...}}
Source code in nextmv/nextmv/local/application.py
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 | |
run_visuals
¶
run_visuals(run_id: str) -> list[str]
Open the local run visuals in a web browser.
This method opens the visual representation of a locally executed run
in the default web browser. It assumes that the run was executed locally
using the new_run or new_run_with_result method and that
the necessary visualization files are present.
If the run was correctly configured to produce visual assets, then the
run will contain a visuals directory with one or more HTML files.
Each file is opened in a new tab in the default web browser.
| PARAMETER | DESCRIPTION |
|---|---|
|
ID of the local run to visualize.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
The local URLs of the visual files that were opened in the web browser. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Source code in nextmv/nextmv/local/application.py
src
instance-attribute
¶
Source of the application, when initialized locally. An application's
source typically refers to the directory containing the app.yaml
manifest.
sync
¶
sync(
target: Application,
run_ids: list[str] | None = None,
instance_id: str | None = None,
verbose: bool | None = False,
rich_print: bool | None = False,
) -> None
Sync the local application to a Nextmv Cloud application target.
The Application class allows you to perform and handle local
application runs with methods such as:
new_runnew_run_with_resultrun_metadatarun_resultrun_result_with_polling
The runs produced locally live under self.src/.nextmv/runs. This
method syncs those runs to a Nextmv Cloud application target, making
them available for remote execution and management.
| PARAMETER | DESCRIPTION |
|---|---|
|
Target Nextmv Cloud application where the local application runs will be synced to.
TYPE:
|
|
List of run IDs to sync. If None, all local runs found under
TYPE:
|
|
Optional instance ID if you want to associate your runs with an instance.
TYPE:
|
|
Whether to print verbose output during the sync process. Useful for debugging a large number of runs being synced.
TYPE:
|
|
Whether to use rich printing for output during the sync process.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
ValueError
|
If the |
ValueError
|
If the application does not exist in Nextmv Cloud. |
ValueError
|
If a run does not exist locally. |
HTTPError
|
If the response status code is not 2xx. |
Source code in nextmv/nextmv/local/application.py
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 | |
update
¶
update(
app_id: str | None = None,
description: str | None = None,
content_format: ContentFormat | None = None,
) -> None
Update the local application.
This method allows you to update the local application's metadata, such as its app_id, description and content format. It does not affect the application's source code or runs.
| PARAMETER | DESCRIPTION |
|---|---|
|
New app_id for the application. If None, the app_id is not updated.
TYPE:
|
|
New description for the application. If None, the description is not updated.
TYPE:
|
|
New content format for the application. If None, the content format is not updated.
TYPE:
|