โ๏ธ Approximate time to complete: 15 min.
In this tutorial you will learn how to use the nextmv.local package for working with Nextmv Applications locally. This tutorial is the same as the getting started with an existing decision model tutorial. Complete this tutorial if you:
- Want to explore Nextmv, by starting with a free alternative.
- Are fluent using Python ๐.
To complete this tutorial, we will use an external example, working under the principle that it is not a Nextmv-created decision model. You can, and should, use your own decision model, or follow along with the example provided:
Let's dive right in ๐คฟ.
1. Prepare the executable code
If you are working with your own decision model and already know that it executes, feel free to skip this step.
The decision model is composed of executable code that solves an optimization problem. Copy the desired example code to a script named main.py.
2. Install requirements
If you are working with your own decision model and already have all requirements ready for it, feel free to skip this step.
Make sure you have the appropriate requirements installed for your model. If you don't have one already, create a requirements.txt file in the root of your project with the Python package requirements needed.
Install the requirements by running the following command:
3. Run the executable code
If you are working with your own decision model and already know that it executes, feel free to skip this step.
Make sure your decision model works by running the executable code.
4. Nextmv-ify the decision model
We are going to turn the executable decision model into a Nextmv Application.
So, what is a Nextmv Application? A Nextmv Application is an entity that contains a decision model as executable code. An Application can make a run by taking an input, executing the decision model, and producing an output. An Application is defined by its code, and a configuration file named app.yaml, known as the "app manifest".
Think of the app as a shell that contains your decision model code, and provides the necessary structure to run it.
A run on a Nextmv Application follows this convention:

- The app receives one, or more, inputs (problem data).
- The app run can be configured through options.
- The app processes the inputs, and executes the decision model.
- The app produces one, or more, outputs (solutions).
- The app optionally produces statistics (metrics) and assets (can be visual, like charts).
We are going to adapt the example so that it can follow these conventions.
Start by adding the app.yaml file, which is known as the app manifest, to the root of the project. This file contains the configuration of the app.
This tutorial is not meant to discuss the app manifest in-depth, for that you can go to the manifest docs. However, these are the main attributes shown in the manifest:
type: it is apythonapplication.runtime: when deployed to Nextmv Cloud, this application can be run on the standardpython:3.11runtime.files: contains files that make up the executable code of the app. In this case, we only need themain.pyfile.python.pip-requirements: specifies the file with the Python packages that need to be installed for the application.
A dependency for nextmv is also added. This dependency is optional, and the modeling constructs are not needed to run a Nextmv Application locally. However, using the SDK modeling features makes it easier to work with Nextmv apps, as a lot of convenient functionality is already baked in, like:
- Reading and interpreting the manifest.
- Easily reading and writing files based on the content format.
- Parsing and using options from the command line, or environment variables.
- Structuring inputs and outputs.
These are the new requirements.txt contents:
Now, you can overwrite your main.py file with the Nextmv-ified version.
This is a short summary of the changes introduced to the example:
- Load the app manifest from the
app.yamlfile. - Extract options (configurations) from the manifest.
- The input data is no longer in the Python file itself. We will move it to a file under
inputs/input.json. In a singlejsonfile we will define the complete input. Given that we are working with thejsoncontent format, we use the Python SDK to load the input data fromstdin. - Modify the definition of data to use the loaded input data.
- Store the solution to the problem, and solver metrics (statistics), in an output.
- Write the output to
stdout, given that we are working with thejsoncontent format.
Here is the data file that you need to place in an inputs directory:
After you are done Nextmv-ifying, your Nextmv app should have the following structure, for the example provided:
You are ready to run your existing Nextmv Application locally using the nextmv.local package ๐ฅณ.
5. Start a run
Let's use the mechanisms provided by the local package to run the app systematically by submitting a couple of runs to the local app, using the local.Application.new_run method.
Create a script named app1.py, or use a cell of a Jupyter notebook. Copy and paste the following code into it, making sure you use the correct app src (for this example, the current working directory, "."):
When you instantiate a local.Application, the src argument must point to a directory where the app.yaml manifest file is located.
This will print the IDs of the runs created. The app runs start in the background. Run the script, or notebook cell, to get an output similar to this:
6. Get a run result
You can get a run result using the run ID with the local.Application.run_result method.
Create another script, which you can name app2.py, or use another cell in the Jupyter notebook. Copy and paste the following code into it, making sure you use the correct run ID (one of the identifiers that were printed in step 5) and app src:
Run the script, or notebook cell, and you should see an output similar to this one:
You'll notice that the .output field contains the same output that is produced by "manually" running the app. However, the run result also contains information about the run, such as its ID, creation time, duration, status, and more.
The Nextmv SDK keeps track of all the local runs you create inside your application.
7. Get run information
Runs may take a while to complete. We recommend you poll for the run status until it is completed. Once the run is completed, you can get the run result as shown above. To get the run information, use the run ID and the local.Application.run_metadata method.
Create another script, which you can name app3.py, or use another cell in the Jupyter notebook. Copy and paste the following code into it, making sure you use the correct run ID (one of the identifiers that were printed in step 5) and app src:
You can get the run information using the run ID.
Run the script, or notebook cell, and you should see an output similar to this one:
As you can see, the run information contains metadata about the run, such as its status, creation time, and more.
8. All in one
Since runs are started in the background, you should poll until the run succeeds (or fails) to get the results. You can use the local.Application.new_run_with_result method to do everything:
- Start a run
- Poll for results
- Return them
Create another script, which you can name app4.py, or use another cell in the Jupyter notebook. Copy and paste the following code into it, making sure you use the correct app src:
Run the script, or notebook cell, and you should see an output similar to the one shown in the getting a run result section.
The complete methodology for running is discussed in detail in the runs tutorial.
9. Visualize run assets
The Nextmv-ified main.py script contains a method called create_route_visualization, which was created by us. This method uses plotly to create a visualization for the given example: the routes assigned to the vehicles, departing and returning to the depot. We are going to use the location coordinates provided by the OR-Tools example to visualize the routes.
Create a new input_with_coordinates.json file in the inputs directory which includes the coordinates:
Up until now, the main.py script has not created any run assets given that no coordinates were given. Now, we can run the app again using this new input file to generate the visualization assets. You can use the local.Application.run_visuals method to visualize the assets of a run.
Create another script, which you can name app5.py, or use another cell in the Jupyter notebook. Copy and paste the following code into it, making sure you use the correct app src:
Run the script, or notebook cell, and you should see an output similar to this one:
The .output.solution.assets field will now contain the visualization assets produced by the create_route_visualization method in the main.py script. The following libraries are supported for visualizing assets with the run_visuals method:
plotlyfolium
This will also open a browser window for each asset produced by the run. You should see a simple plot like the following:

The run_visuals method may not work as expected in a Jupyter notebook environment. We recommend running it from a script executed in a terminal.
10. Understanding what happened
If you inspect the application directory consolidated in step 4 again, you will see a structure similar to the following:
The .nextmv dir is used to store and manage the local applications runs in a structured way. The local package is used to interact with these files, with methods for starting runs, retrieving results, visualizing charts, and more.
๐๐๐ Congratulations, you have finished this tutorial!
Full tutorial code
You can find the consolidated code examples used in this tutorial in the tutorials GitHub repository. The free-local-experience dir contains all the code that was shown in this tutorial.
For the example, you will find two directories:
original: the original example without any modifications.nextmv-ified: the example converted into a Nextmv Application.
Go into each directory for instructions about running the decision model.