panel.io.resources module#

Patches bokeh resources to make it easy to add external JS and CSS resources via the panel.config object.

class panel.io.resources.ResourceComponent[source]#

Bases: object

Mix-in class for components that define a set of resources that have to be resolved.

Methods

resolve_resources([cdn, extras])

Resolves the resources required for this component.

resolve_resources(cdn: bool | t.Literal['auto'] = 'auto', extras: dict[str, dict[str, str]] | None = None) → ResourcesType[source]#

Resolves the resources required for this component.

Parameters:
cdn: bool | Literal[‘auto’]

Whether to load resources from CDN or local server. If set to ‘auto’ value will be automatically determine based on global settings.

extras: dict[str, dict[str, str]] | None

Additional resources to add to the bundle. Valid resource types include js, js_modules and css.

Returns:
Dictionary containing JS and CSS resources.
class panel.io.resources.Resources(*args, absolute=False, notebook=False, **kwargs)[source]#

Bases: Resources

Attributes:
css_files
css_raw
dist_dir
js_files
js_module_exports
js_modules
js_raw
render_js
resource_declarations

Methods

adjust_paths(resources)

Computes relative and absolute paths for resources.

clone(*[, components])

Make a clone of a resources instance allowing to override its components.

extra_resources(resources, resource_type)

Adds resources for ReactiveHTML components.

from_bokeh

adjust_paths(resources)[source]#

Computes relative and absolute paths for resources.

clone(*, components=None) → Resources[source]#

Make a clone of a resources instance allowing to override its components.

extra_resources(resources, resource_type)[source]#

Adds resources for ReactiveHTML components.

panel.io.resources.component_resource_path(component, attr: str, path: str | PathLike) → str[source]#

Generates a canonical URL for a component resource.

To be used in conjunction with the panel.io.server.ComponentResourceHandler which allows dynamically resolving resources defined on components.

panel.io.resources.extension_declared(cls: type) → bool[source]#

Whether a model class’ extension was declared in this session.

Classes that do not belong to an extension always pass, as does every class when there is no session to ask (a notebook, or rendering outside a document), which is what keeps those paths eager.

panel.io.resources.get_env()[source]#

Get the correct Jinja2 Environment, also for frozen scripts.

panel.io.resources.get_notebook_resources() → bool[source]#

Whether resource urls should use the Jupyter extension endpoint.

panel.io.resources.get_resource_mode() → MODES[source]#

The mode urls are currently being resolved for.

Has to be read through a function: set_resource_mode rebinds the module global, so a module that imported the name would keep whatever it happened to be at import time.

class panel.io.resources.json_dumps(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: JSONEncoder

default(obj)[source]#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
panel.io.resources.module_tags(js_modules: list[str], js_module_exports: dict[str, str]) → list[tuple[str, str | None]][source]#

Pairs each module url with the global its namespace is exported as.

A module that has an export wrapper is loaded by that wrapper’s import, so emitting a bare <script type="module" src> for it as well leaves two tags for one url. Pairing them keeps every module in the order js_modules gives, which is the order they execute in.

panel.io.resources.patch_model_css(root: Model, dist_url: str)[source]#

Temporary patch for Model.css property used by Panel to provide stylesheets for components.

ALERT: Should find better solution before official Bokeh 3.x compatible release

panel.io.resources.process_raw_css(raw_css: list[str]) → list[str][source]#

Converts old-style Bokeh<3 compatible CSS to Bokeh 3 compatible CSS.

panel.io.resources.resolve_custom_path(obj, path: str | PathLike, relative: bool = False) → Path | None[source]#

Attempts to resolve a path relative to some component.

Parameters:
obj: type | object

The component to resolve the path relative to.

path: str | os.PathLike

Absolute or relative path to a resource.

relative: bool

Whether to return a relative path.

Returns:
path: pathlib.Path | None
panel.io.resources.resolve_resource_cdn(resource: str | PathLike) → str | PathLike[source]#

Resolves a CDN URL given a file path that is relative to an extension dist directory.

panel.io.resources.resolve_stylesheet(cls, stylesheet: str, attribute: str | None = None)[source]#

Resolves a stylesheet definition, e.g. originating on a component Reactive._stylesheets or a Design.modifiers attribute. Stylesheets may be defined as one of the following:

  • Absolute URL defined with http(s) protocol

  • A path relative to the component

  • A raw css string

Parameters:
cls: type | object

Object or class defining the stylesheet

stylesheet: str

The stylesheet definition

panel.io.resources.set_default_resource_mode(mode: MODES, *, notebook: bool = False)[source]#

Sets the mode urls are resolved for outside a set_resource_mode block.

The module default is server, i.e. relative urls into the static endpoint Panel itself serves, which is the right guess for an application but wrong for a notebook: the page is served by Jupyter from an unrelated url, so static/extensions/panel/... resolves against /lab/tree/<dir>/ and 404s. It cannot be handled by a context manager either, because components created in a later cell resolve their resources long after pn.extension() returned.

panel.io.resources.stylesheet_url(stylesheet: ImportedStyleSheet) → str | None[source]#

Returns the url of an ImportedStyleSheet or None if it is unset.

Bokeh clears the property values of every model on a Document when the Document is destroyed at the end of a session. An ImportedStyleSheet that outlives the Document it was rendered into, e.g. because it is held in a cache, is therefore left without a url and reading it raises an UnsetValueError. Such a stylesheet must never be reused since it breaks both model creation and the serialization of the Document it is added to.