| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
# Standard library imports. |
| 12 |
import codecs |
| 13 |
import os.path |
| 14 |
|
| 15 |
# Enthought library imports. |
| 16 |
from pyface.tasks.api import TraitsDockPane |
| 17 |
from traits.api import cached_property, HasTraits, Instance, Property, Str |
| 18 |
from traitsui.api import HTMLEditor, Item, View |
| 19 |
|
| 20 |
# Constants. |
| 21 |
HELP_PATH = os.path.join(os.path.dirname(__file__), "help") |
| 22 |
|
| 23 |
|
| 24 |
class ModelHelpPane(TraitsDockPane): |
| 25 |
"""A dock pane for viewing any help associated with a model.""" |
| 26 |
|
| 27 |
#### 'ITaskPane' interface ################################################ |
| 28 |
|
| 29 |
id = "example.attractors.model_help_pane" |
| 30 |
name = "Model Information" |
| 31 |
|
| 32 |
#### 'ModelConfigPane' interface ########################################## |
| 33 |
|
| 34 |
model = Instance(HasTraits) |
| 35 |
|
| 36 |
html = Property(Str, observe="model") |
| 37 |
|
| 38 |
view = View( |
| 39 |
Item( |
| 40 |
"pane.html", |
| 41 |
editor=HTMLEditor(base_url=HELP_PATH, open_externally=True), |
| 42 |
show_label=False, |
| 43 |
), |
| 44 |
width=300, |
| 45 |
resizable=True, |
| 46 |
) |
| 47 |
|
| 48 |
########################################################################### |
| 49 |
# Protected interface. |
| 50 |
########################################################################### |
| 51 |
|
| 52 |
@cached_property |
| 53 |
def _get_html(self): |
| 54 |
"""Fetch the help HTML for the current model.""" |
| 55 |
if self.model is None: |
| 56 |
return "No model selected." |
| 57 |
|
| 58 |
# Determine the name of the model. |
| 59 |
model = self.model |
| 60 |
while hasattr(model, "adaptee"): |
| 61 |
model = model.adaptee |
| 62 |
name = model.__class__.__name__.lower() |
| 63 |
|
| 64 |
# Load HTML file, if possible. |
| 65 |
path = os.path.join(HELP_PATH, name + ".html") |
| 66 |
if os.path.isfile(path): |
| 67 |
with codecs.open(path, "r", "utf-8") as f: |
| 68 |
return f.read() |
| 69 |
else: |
| 70 |
return "No information available for model." |