viewable Module#

viewable Module#

Defines the baseclasses that make a component render to a bokeh model and become viewable including:

  • Layoutable: Defines parameters concerned with layout and style

  • ServableMixin: Mixin class that defines methods to serve object on server

  • Renderable: Defines methods to render a component as a bokeh model

  • Viewable: Defines methods to view the component in the notebook, on the server or in static exports

class panel.viewable.Layoutable(**params)[source]#

Bases: Parameterized

Layoutable defines shared style and layout related parameters for all Panel components with a visual representation.

align = param.ClassSelector(class_=(<class ‘str’>, <class ‘tuple’>), default=’start’, label=’Align’)

Whether the object should be aligned with the start, end or center of its container. If set as a tuple it will declare (vertical, horizontal) alignment.

aspect_ratio = param.Parameter(allow_None=True, label=’Aspect ratio’)

Describes the proportional relationship between component’s width and height. This works if any of component’s dimensions are flexible in size. If set to a number, width / height =         aspect_ratio relationship will be maintained. Otherwise, if set to "auto", component’s preferred width and height will be used to determine the aspect (if not set, no aspect will be preserved).

background = param.Parameter(allow_None=True, label=’Background’)

Background color of the component.

css_classes = param.List(allow_None=True, bounds=(0, None), label=’Css classes’)

CSS classes to apply to the layout.

width = param.Integer(allow_None=True, bounds=(0, None), inclusive_bounds=(True, True), label=’Width’)

The width of the component (in pixels). This can be either fixed or preferred width, depending on width sizing policy.

height = param.Integer(allow_None=True, bounds=(0, None), inclusive_bounds=(True, True), label=’Height’)

The height of the component (in pixels). This can be either fixed or preferred height, depending on height sizing policy.

min_width = param.Integer(allow_None=True, bounds=(0, None), inclusive_bounds=(True, True), label=’Min width’)

Minimal width of the component (in pixels) if width is adjustable.

min_height = param.Integer(allow_None=True, bounds=(0, None), inclusive_bounds=(True, True), label=’Min height’)

Minimal height of the component (in pixels) if height is adjustable.

max_width = param.Integer(allow_None=True, bounds=(0, None), inclusive_bounds=(True, True), label=’Max width’)

Minimal width of the component (in pixels) if width is adjustable.

max_height = param.Integer(allow_None=True, bounds=(0, None), inclusive_bounds=(True, True), label=’Max height’)

Minimal height of the component (in pixels) if height is adjustable.

margin = param.Parameter(default=5, label=’Margin’)

Allows to create additional space around the component. May be specified as a two-tuple of the form (vertical, horizontal) or a four-tuple (top, right, bottom, left).

width_policy = param.ObjectSelector(default=’auto’, label=’Width policy’, objects=[‘auto’, ‘fixed’, ‘fit’, ‘min’, ‘max’])

Describes how the component should maintain its width. "auto" Use component’s preferred sizing policy. "fixed" Use exactly width pixels. Component will overflow if it can’t fit in the available horizontal space. "fit" Use component’s preferred width (if set) and allow it to fit into the available horizontal space within the minimum and maximum width bounds (if set). Component’s width neither will be aggressively minimized nor maximized. "min" Use as little horizontal space as possible, not less than the minimum width (if set). The starting point is the preferred width (if set). The width of the component may shrink or grow depending on the parent layout, aspect management and other factors. "max" Use as much horizontal space as possible, not more than the maximum width (if set). The starting point is the preferred width (if set). The width of the component may shrink or grow depending on the parent layout, aspect management and other factors.

height_policy = param.ObjectSelector(default=’auto’, label=’Height policy’, objects=[‘auto’, ‘fixed’, ‘fit’, ‘min’, ‘max’])

Describes how the component should maintain its height. "auto" Use component’s preferred sizing policy. "fixed" Use exactly height pixels. Component will overflow if it can’t fit in the available vertical space. "fit" Use component’s preferred height (if set) and allow to fit into the available vertical space within the minimum and maximum height bounds (if set). Component’s height neither will be aggressively minimized nor maximized. "min" Use as little vertical space as possible, not less than the minimum height (if set). The starting point is the preferred height (if set). The height of the component may shrink or grow depending on the parent layout, aspect management and other factors. "max" Use as much vertical space as possible, not more than the maximum height (if set). The starting point is the preferred height (if set). The height of the component may shrink or grow depending on the parent layout, aspect management and other factors.

sizing_mode = param.ObjectSelector(label=’Sizing mode’, objects=[‘fixed’, ‘stretch_width’, ‘stretch_height’, ‘stretch_both’, ‘scale_width’, ‘scale_height’, ‘scale_both’, None])

How the component should size itself. This is a high-level setting for maintaining width and height of the component. To gain more fine grained control over sizing, use width_policy, height_policy and aspect_ratio instead (those take precedence over sizing_mode). "fixed" Component is not responsive. It will retain its original width and height regardless of any subsequent browser window resize events. "stretch_width" Component will responsively resize to stretch to the available width, without maintaining any aspect ratio. The height of the component depends on the type of the component and may be fixed or fit to component’s contents. "stretch_height" Component will responsively resize to stretch to the available height, without maintaining any aspect ratio. The width of the component depends on the type of the component and may be fixed or fit to component’s contents. "stretch_both" Component is completely responsive, independently in width and height, and will occupy all the available horizontal and vertical space, even if this changes the aspect ratio of the component. "scale_width" Component will responsively resize to stretch to the available width, while maintaining the original or provided aspect ratio. "scale_height" Component will responsively resize to stretch to the available height, while maintaining the original or provided aspect ratio. "scale_both" Component will responsively resize to both the available width and height, while maintaining the original or provided aspect ratio.

visible = param.Boolean(bounds=(0, 1), default=True, label=’Visible’)

Whether the component is visible. Setting visible to false will hide the component entirely.

class panel.viewable.Viewable(**params)[source]#

Bases: Renderable, Layoutable, ServableMixin

Viewable is the baseclass all visual components in the panel library are built on. It defines the interface for declaring any object that displays itself by transforming the object(s) being wrapped into models that can be served using bokeh’s layout engine. The class also defines various methods that allow Viewable objects to be displayed in the notebook and on bokeh server.

Parameters inherited from:

panel.viewable.Layoutable: align, aspect_ratio, background, css_classes, width, height, min_width, min_height, max_width, max_height, margin, width_policy, height_policy, sizing_mode, visible

loading = param.Boolean(bounds=(0, 1), default=False, label=’Loading’)

Whether or not the Viewable is loading. If True a loading spinner is shown on top of the Viewable.

app(notebook_url: str = 'localhost:8888', port: int = 0) Server[source]#

Displays a bokeh server app inline in the notebook.

Parameters
  • notebook_url (str) – URL to the notebook server

  • port (int (optional, default=0)) – Allows specifying a specific port

clone(**params) Viewable[source]#

Makes a copy of the object sharing the same parameters.

Parameters

params (Keyword arguments override the parameters on the clone.) –

Return type

Cloned Viewable object

embed(max_states: int = 1000, max_opts: int = 3, json: bool = False, json_prefix: str = '', save_path: str = './', load_path: Optional[str] = None, progress: bool = False, states={}) None[source]#

Renders a static version of a panel in a notebook by evaluating the set of states defined by the widgets in the model. Note this will only work well for simple apps with a relatively small state space.

Parameters
  • max_states (int) – The maximum number of states to embed

  • max_opts (int) – The maximum number of states for a single widget

  • json (boolean (default=True)) – Whether to export the data to json files

  • json_prefix (str (default='')) – Prefix for JSON filename

  • save_path (str (default='./')) – The path to save json files to

  • load_path (str (default=None)) – The path or URL the json files will be loaded from.

  • progress (boolean (default=False)) – Whether to report progress

  • states (dict (default={})) – A dictionary specifying the widget values to embed for each widget

get_root(doc: Optional[Document] = None, comm: Optional[Comm] = None, preprocess: bool = True) Model#

Returns the root model and applies pre-processing hooks

Parameters
  • doc (bokeh.Document) – Bokeh document the bokeh model will be attached to.

  • comm (pyviz_comms.Comm) – Optional pyviz_comms when working in notebook

  • preprocess (boolean (default=True)) – Whether to run preprocessing hooks

Return type

Returns the bokeh model corresponding to this panel object

pprint() None[source]#

Prints a compositional repr of the class.

save(filename: str | os.PathLike | IO, title: Optional[str] = None, resources: Resources | None = None, template: str | Template | None = None, template_variables: Dict[str, Any] = {}, embed: bool = False, max_states: int = 1000, max_opts: int = 3, embed_json: bool = False, json_prefix: str = '', save_path: str = './', load_path: Optional[str] = None, progress: bool = True, embed_states: Dict[Any, Any] = {}, as_png: bool | None = None, **kwargs) None[source]#

Saves Panel objects to file.

Parameters
  • filename (str or file-like object) – Filename to save the plot to

  • title (string) – Optional title for the plot

  • resources (bokeh resources) – One of the valid bokeh.resources (e.g. CDN or INLINE)

  • template – passed to underlying io.save

  • template_variables – passed to underlying io.save

  • embed (bool) – Whether the state space should be embedded in the saved file.

  • max_states (int) – The maximum number of states to embed

  • max_opts (int) – The maximum number of states for a single widget

  • embed_json (boolean (default=True)) – Whether to export the data to json files

  • json_prefix (str (default='')) – Prefix for the auto-generated json directory

  • save_path (str (default='./')) – The path to save json files to

  • load_path (str (default=None)) – The path or URL the json files will be loaded from.

  • progress (boolean (default=True)) – Whether to report progress

  • embed_states (dict (default={})) – A dictionary specifying the widget values to embed for each widget

  • as_png (boolean (default=None)) – To save as a .png. If None save_png will be true if filename is string and ends with png.

select(selector: Optional[type | Callable[['Viewable'], bool]] = None) List['Viewable'][source]#

Iterates over the Viewable and any potential children in the applying the Selector.

Parameters

selector (type or callable or None) – The selector allows selecting a subset of Viewables by declaring a type or callable function to filter by.

Returns

viewables

Return type

list(Viewable)

servable(title: Optional[str] = None, location: bool | 'Location' = True, area: str = 'main', target: Optional[str] = None) _Self#

Serves the object or adds it to the configured pn.state.template if in a panel serve context, writes to the DOM if in a pyodide context and returns the Panel object to allow it to display itself in a notebook context.

Parameters
  • title (str) – A string title to give the Document (if served as an app)

  • location (boolean or panel.io.location.Location) – Whether to create a Location component to observe and set the URL location.

  • area (str (deprecated)) – The area of a template to add the component too. Only has an effect if pn.config.template has been set.

  • target (str) – Target area to write to. If a template has been configured on pn.config.template this refers to the target area in the template while in pyodide this refers to the ID of the DOM node to write to.

Return type

The Panel object itself

server_doc(doc: Optional[Document] = None, title: Optional[str] = None, location: bool | 'Location' = True) Document[source]#

Returns a serveable bokeh Document with the panel attached

Parameters
  • doc (bokeh.Document (optional)) – The bokeh Document to attach the panel to as a root, defaults to bokeh.io.curdoc()

  • title (str) – A string title to give the Document

  • location (boolean or panel.io.location.Location) – Whether to create a Location component to observe and set the URL location.

Returns

doc – The bokeh document the panel was attached to

Return type

bokeh.Document

show(title: Optional[str] = None, port: int = 0, address: Optional[str] = None, websocket_origin: Optional[str] = None, threaded: bool = False, verbose: bool = True, open: bool = True, location: bool | 'Location' = True, **kwargs) threading.Thread | 'Server'#

Starts a Bokeh server and displays the Viewable in a new tab.

Parameters
  • title (str | None) – A string title to give the Document (if served as an app)

  • port (int (optional, default=0)) – Allows specifying a specific port

  • address (str) – The address the server should listen on for HTTP requests.

  • websocket_origin (str or list(str) (optional)) – A list of hosts that can connect to the websocket. This is typically required when embedding a server app in an external web site. If None, “localhost” is used.

  • threaded (boolean (optional, default=False)) – Whether to launch the Server on a separate thread, allowing interactive use.

  • verbose (boolean (optional, default=True)) – Whether to print the address and port

  • open (boolean (optional, default=True)) – Whether to open the server in a new browser tab

  • location (boolean or panel.io.location.Location) – Whether to create a Location component to observe and set the URL location.

Returns

server – Returns the Bokeh server instance or the thread the server was launched on (if threaded=True)

Return type

bokeh.server.Server or threading.Thread

class panel.viewable.Viewer(*, fig, holomap, widgets, fps, max_frames, size, dpi, filename, info, css, widget_location)[source]#

Bases: Parameterized

A baseclass for custom components that behave like a Panel object. By implementing __panel__ method an instance of this class will behave like the returned Panel component when placed in a layout, render itself in a notebook and provide show and servable methods.

servable(title: Optional[str] = None, location: bool | 'Location' = True, area: str = 'main', target: Optional[str] = None) Viewable[source]#

Serves the object or adds it to the configured pn.state.template if in a panel serve context, writes to the DOM if in a pyodide context and returns the Panel object to allow it to display itself in a notebook context.

Parameters
  • title (str) – A string title to give the Document (if served as an app)

  • location (boolean or panel.io.location.Location) – Whether to create a Location component to observe and set the URL location.

  • area (str (deprecated)) – The area of a template to add the component too. Only has an effect if pn.config.template has been set.

  • target (str) – Target area to write to. If a template has been configured on pn.config.template this refers to the target area in the template while in pyodide this refers to the ID of the DOM node to write to.

Return type

The Panel object itself

show(title: Optional[str] = None, port: int = 0, address: Optional[str] = None, websocket_origin: Optional[str] = None, threaded: bool = False, verbose: bool = True, open: bool = True, location: bool | 'Location' = True, **kwargs) threading.Thread | 'Server'[source]#

Starts a Bokeh server and displays the Viewable in a new tab.

Parameters
  • title (str | None) – A string title to give the Document (if served as an app)

  • port (int (optional, default=0)) – Allows specifying a specific port

  • address (str) – The address the server should listen on for HTTP requests.

  • websocket_origin (str or list(str) (optional)) – A list of hosts that can connect to the websocket. This is typically required when embedding a server app in an external web site. If None, “localhost” is used.

  • threaded (boolean (optional, default=False)) – Whether to launch the Server on a separate thread, allowing interactive use.

  • verbose (boolean (optional, default=True)) – Whether to print the address and port

  • open (boolean (optional, default=True)) – Whether to open the server in a new browser tab

  • location (boolean or panel.io.location.Location) – Whether to create a Location component to observe and set the URL location.

Returns

server – Returns the Bokeh server instance or the thread the server was launched on (if threaded=True)

Return type

bokeh.server.Server or threading.Thread