Skip to content

Commit

Permalink
Merge pull request OpenMDAO#2970 from robfalck/show_options_table_update
Browse files Browse the repository at this point in the history
Added argument to show_options_table to allow the attribute name of the options table to be passed.
  • Loading branch information
swryan authored Jul 10, 2023
2 parents cf24fa6 + 537b9e0 commit 8f7968a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"outputs": [],
"source": [
"import openmdao.api as om\n",
"om.show_options_table(\"openmdao.core.problem.Problem\", recording_options=True)"
"om.show_options_table(\"openmdao.core.problem.Problem\", options_dict=\"recording_options\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"outputs": [],
"source": [
"import openmdao.api as om\n",
"om.show_options_table(\"openmdao.solvers.solver.Solver\", recording_options=True)"
"om.show_options_table(\"openmdao.solvers.solver.Solver\", options_dict=\"recording_options\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"outputs": [],
"source": [
"import openmdao.api as om\n",
"om.show_options_table(\"openmdao.core.system.System\", recording_options=True)"
"om.show_options_table(\"openmdao.core.system.System\", options_dict=\"recording_options\")"
]
},
{
Expand Down
20 changes: 13 additions & 7 deletions openmdao/utils/notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ipy = display = HTML = IFrame = None

from openmdao.utils.om_warnings import issue_warning
from openmdao.utils.om_warnings import warn_deprecation

colab = 'google.colab' in sys.modules

Expand Down Expand Up @@ -89,17 +90,19 @@ def display_source(reference, hide_doc_string=False):
display(get_code(reference, hide_doc_string))


def show_options_table(reference, recording_options=False):
def show_options_table(reference, recording_options=False, options_dict='options'):
"""
Return the options table of the given reference path.
Parameters
----------
reference : str or object
Dot path of desired class or function or an instance.
recording_options : bool
If True, display recording options instead of options.
options_dict : str
The string name of the attribute of the reference object
that provides to OptionsDictionary.
Returns
-------
Expand All @@ -112,12 +115,15 @@ def show_options_table(reference, recording_options=False):
obj = reference

if ipy:
if not hasattr(obj, "options"):
opt = obj
elif not recording_options:
opt = obj.options
else:
if recording_options:
warn_deprecation('Argument `recording_options` is deprecated. Use '
'`options_dict="recording_options" to remove this '
'warning.')
opt = obj.recording_options
elif hasattr(obj, options_dict):
opt = getattr(obj, options_dict)
else:
raise AttributeError(f'Object {reference} has no attribute {options_dict}.')

return display(HTML(str(opt.to_table(fmt='html', display=False))))
else:
Expand Down
7 changes: 5 additions & 2 deletions openmdao/utils/tests/test_notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def test_show_obj_options(self):
from openmdao.utils import notebook_utils
notebook_utils.ipy = True

options = om.show_options_table("openmdao.utils.tests.test_notebook_utils.StateOptionsDictionary")
with self.assertRaises(AttributeError) as e:
om.show_options_table("openmdao.utils.tests.test_notebook_utils.StateOptionsDictionary")

self.assertEqual(options, None)
self.assertEqual(str(e.exception),
'Object openmdao.utils.tests.test_notebook_utils.StateOptionsDictionary '
'has no attribute options.')

def test_show_options_w_attr(self):
from openmdao.utils import notebook_utils
Expand Down

0 comments on commit 8f7968a

Please sign in to comment.