Source code for neuralib.dashboard.examples.view_animal

from typing import Optional, Union

from bokeh.model import Model
from bokeh.models.widgets.inputs import Select
from bokeh.models.widgets.markups import Div
from neuralib.dashboard import BokehServer, View

from .view_figure import AnimalFigureView

__all__ = ['AnimalView']


[docs] class AnimalView(View): select: Select content: Div
[docs] def __init__(self, animal: str | None = None): self._animal: None | str | BaseException = animal
@property def title(self) -> str: animal = self.current_animal or 'Unknown' return f'Animal {animal}' @property def current_animal(self) -> str | None: if self._animal is None: try: self._animal = self.get_arg('animal')[0] except (OSError, KeyError) as e: self._animal = e if isinstance(self._animal, str): return self._animal else: return None
[docs] def setup(self) -> Model: self.select = Select( title='Figure Type', value='', options=['', *AnimalFigureView.FIGURE_TYPE] ) self.select.on_change('value', self.on_figure_type) self.content = Div(text="", css_classes=['yw-animal-content']) from bokeh.layouts import column return column( self.select, self.content, Div(text=""" <style> div.yw-animal-content div { position: fixed; width: 90%; height: 100%; } div.yw-animal-content div iframe.bk { display: block; width: 100%; height: 100%; } </style> """) )
[docs] def on_figure_type(self, attr: str, old: str, value: str): if len(value) == 0: self.content.text = '' return animal = self.current_animal if animal is None: return self.content.text = f""" <iframe src="/pool/figure?animal={animal}&figure={value}" class="bk" ></iframe> """
def main(): import argparse ap = argparse.ArgumentParser() ap.add_argument('--animal', metavar='NAME', required=True) opt = ap.parse_args() BokehServer().start(AnimalView(opt.animal)) if __name__ == '__main__': main()