Add a model viewer option to specify the model by variable name in __main__ #3271
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary/Motivation:
When you are working with the model viewer in Jupyter (Lab, qtconsole...) usually your Pyomo model is stored in main. This isn't necessarily the case, but it is more than likely, and if we assume that is the case we can do a few things to make the viewer easier to work with. This adds an option to specify the model by its variable name in main rather than directly specifying the model by pointer. This is just a new option and doesn't change the default behavior. In jupyter there is a callback that causes the model view to refresh every time you run a command. When you use the new option, the refresh will also check the model id to see if it has changed and load the new model if it has.
This also changes what happens if you open the UI without specifying a model. It looks in the main namespace and pulls out the first pyomo model it finds based on the order of dir(main), which I think is alphabetical. In the qtconsole-based model-viewer, it no longer creates an empty model if it can't find one, it just shows the viewer empty, and you'll have to point it at a model after you create one.
The main motivation for this is that it saves me (and hopefully anyone else that uses the viewer) a lot of time selecting the model in the UI. A lot of times I create a model by running a script. This lets me run the script and open the UI without explicitly selecting the model. Then I often find something need changed in the model which is easier done by updating the script. This lets me rerun the script overwriting the model and having the UI update without having to go back and select the model again. This also avoids confusion where the user may not realize they are looking at an older version of the model.
Changes proposed in this PR:
Legal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: