Skip to content

Commit

Permalink
Micro optimizations for Viewable (#6074)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Dec 18, 2023
1 parent 71726bb commit 6e09749
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
29 changes: 9 additions & 20 deletions panel/chat/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,18 @@ class ChatReactionIcons(CompositeWidget):
>>> ChatReactionIcons(value=["like"], options={"like": "thumb-up", "dislike": "thumb-down"})
"""

active_icons = param.Dict(
default={},
doc="""
The mapping of reactions to their corresponding active icon names;
if not set, the active icon name will default to its "filled" version.""",
)
active_icons = param.Dict(default={}, doc="""
The mapping of reactions to their corresponding active icon names.
If not set, the active icon name will default to its "filled" version.""")

css_classes = param.List(default=["reaction-icons"], doc="The CSS classes of the widget.")

options = param.Dict(
default={"favorite": "heart"},
doc="""
options = param.Dict(default={"favorite": "heart"}, doc="""
A key-value pair of reaction values and their corresponding tabler icon names
found on https://tabler-icons.io.""",
)
found on https://tabler-icons.io.""")

value = param.List(doc="The active reactions.")

css_classes = param.List(default=["reaction-icons"], doc="The CSS classes of the widget.")

_rendered_icons = param.Dict(
default={},
doc="""
The rendered icons mapping reaction to icon.""",
)

_stylesheets: ClassVar[List[str]] = [f"{CDN_DIST}css/chat_reaction_icons.css"]

_composite_type = Column
Expand Down Expand Up @@ -103,10 +91,11 @@ def _update_value(self, event):


class ChatCopyIcon(ReactiveHTML):
value = param.String(default=None, doc="The text to copy to the clipboard.")

fill = param.String(default="none", doc="The fill color of the icon.")

value = param.String(default=None, doc="The text to copy to the clipboard.")

_template = """
<div
type="button"
Expand Down
12 changes: 5 additions & 7 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,15 @@ def __init__(self, object=None, **params):
reaction_icons = params.get("reaction_icons", {"favorite": "heart"})
if isinstance(reaction_icons, dict):
params["reaction_icons"] = ChatReactionIcons(
options=reaction_icons, width=15, height=15
options=reaction_icons, width=15, height=15,
value=params.get('reactions', []),
)
self._internal = True
super().__init__(object=object, **params)
self.reaction_icons.link(
self, value="reactions", visible="show_reaction_icons",
bidirectional=True
)
self.param.trigger("reactions", "show_reaction_icons")
self.reaction_icons.link(self, value="reactions", bidirectional=True)
self.reaction_icons.visible = self.param.show_reaction_icons
if not self.avatar:
self.param.trigger("avatar_lookup")
self._update_avatar()
self._build_layout()

def _build_layout(self):
Expand Down
23 changes: 15 additions & 8 deletions panel/viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
from __future__ import annotations

import asyncio
import functools
import logging
import os
import sys
import threading
import traceback
import uuid

from functools import partial
from typing import (
IO, TYPE_CHECKING, Any, Callable, ClassVar, Dict, List, Mapping, Optional,
Type,
)

import param # type: ignore
Expand Down Expand Up @@ -55,6 +56,7 @@

from .io.location import Location
from .io.server import StoppableThread
from .theme import Design


class Layoutable(param.Parameterized):
Expand Down Expand Up @@ -507,9 +509,9 @@ def _render_mimebundle(self, model: Model, doc: Document, comm: Comm, location:
ref = model.ref['id']
manager = CommManager(comm_id=comm.id, plot_id=ref)
client_comm = state._comm_manager.get_client_comm(
on_msg=partial(self._on_msg, ref, manager),
on_error=partial(self._on_error, ref),
on_stdout=partial(self._on_stdout, ref)
on_msg=functools.partial(self._on_msg, ref, manager),
on_error=functools.partial(self._on_error, ref),
on_stdout=functools.partial(self._on_stdout, ref)
)
self._comms[ref] = (comm, client_comm)
manager.client_comm_id = client_comm.id
Expand Down Expand Up @@ -696,7 +698,8 @@ def __init__(self, **params):
super().__init__(**params)
self._hooks = hooks

self._update_loading()
if self.loading:
self._update_loading()
self._update_background()
self._update_design()
self._internal_callbacks.extend([
Expand All @@ -705,15 +708,19 @@ def __init__(self, **params):
self.param.watch(self._update_loading, 'loading')
])

@staticmethod
@functools.cache
def _instantiate_design(design: Type[Design], theme: str) -> Design:
return design(theme=theme)

def _update_design(self, *_):
from .theme import Design
from .theme.native import Native
if isinstance(self.design, Design):
self._design = self.design
elif self.design:
self._design = self.design(theme=config.theme)
else:
self._design = Native(theme=config.theme)
design = self.design or Native
self._design = self._instantiate_design(design, config.theme)

def _update_loading(self, *_) -> None:
if self.loading:
Expand Down

0 comments on commit 6e09749

Please sign in to comment.