Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOT TO BE MERGED: New model example #7408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions panel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ class panel_extension(_pyviz_extension):
_imports = {
'ace': 'panel.models.ace',
'codeeditor': 'panel.models.ace',
'chartjs': 'panel.models.chartjs',
'deckgl': 'panel.models.deckgl',
'echarts': 'panel.models.echarts',
'filedropper': 'panel.models.file_dropper',
Expand Down
1 change: 1 addition & 0 deletions panel/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
files.
"""
from .browser import BrowserInfo # noqa
from .chartjs import ChartJS # noqa
from .datetime_picker import DatetimePicker # noqa
from .esm import AnyWidgetComponent, ReactComponent, ReactiveESM # noqa
from .feed import Feed # noqa
Expand Down
10 changes: 10 additions & 0 deletions panel/models/chartjs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from bokeh.core.properties import Int, String

from .layout import HTMLBox


class ChartJS(HTMLBox):
"""Custom ChartJS Model"""

object = String()
clicks = Int()
61 changes: 61 additions & 0 deletions panel/models/chartjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// See https://docs.bokeh.org/en/latest/docs/reference/models/layouts.html
import { HTMLBox, HTMLBoxView } from "./layout"
import {div} from "@bokehjs/core/dom"

// See https://docs.bokeh.org/en/latest/docs/reference/core/properties.html
import * as p from "@bokehjs/core/properties"

// The view of the Bokeh extension/ HTML element
// Here you can define how to render the model as well as react to model changes or View events.
export class ChartJSView extends HTMLBoxView {
declare model: ChartJS
container: Element
objectElement: any // Element

override connect_signals(): void {
super.connect_signals()

this.on_change(this.model.properties.object, () => {
this.render();
})
}

override render(): void {
super.render()
this.container = div({style: {height: "100%", width: "100%"}})
this.container.innerHTML = `<button type="button">${this.model.object}</button>`
this.objectElement = this.container.firstElementChild
this.objectElement.addEventListener("click", () => {this.model.clicks+=1;}, false)
this.shadow_el.append(this.container)
}
}

export namespace ChartJS {
export type Attrs = p.AttrsOf<Props>
export type Props = HTMLBox.Props & {
object: p.Property<string>,
clicks: p.Property<number>,
}
}

export interface ChartJS extends ChartJS.Attrs { }

// The Bokeh .ts model corresponding to the Bokeh .py model
export class ChartJS extends HTMLBox {
declare properties: ChartJS.Props

constructor(attrs?: Partial<ChartJS.Attrs>) {
super(attrs)
}

static override __module__ = "panel.models.chartjs"

static {
this.prototype.default_view = ChartJSView;

this.define<ChartJS.Props>(({Int, String}) => ({
object: [String, "Click Me!"],
clicks: [Int, 0],
}))
}
}
1 change: 1 addition & 0 deletions panel/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {Button} from "./button"
export {ButtonIcon} from "./button_icon"
export {ClickableIcon} from "./icon"
export {Card} from "./card"
export {ChartJS} from "./chartjs"
export {CheckboxButtonGroup} from "./checkbox_button_group"
export {ChatAreaInput} from "./chatarea_input"
export {Column} from "./column"
Expand Down
2 changes: 2 additions & 0 deletions panel/pane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""
from .alert import Alert # noqa
from .base import Pane, PaneBase, panel # noqa
from .chartjs import ChartJS
from .deckgl import DeckGL # noqa
from .echarts import ECharts # noqa
from .equation import LaTeX # noqa
Expand Down Expand Up @@ -63,6 +64,7 @@
"Alert",
"Audio",
"Bokeh",
"ChartJS",
"DataFrame",
"DeckGL",
"ECharts",
Expand Down
38 changes: 38 additions & 0 deletions panel/pane/chartjs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import (
TYPE_CHECKING, ClassVar, Mapping, Optional,
)

import param

from ..models import ChartJS as _BkChartJS
from ..util import lazy_load
from .base import ModelPane

if TYPE_CHECKING:
from bokeh.document import Document
from bokeh.model import Model
from pyviz_comms import Comm, JupyterComm


class ChartJS(ModelPane):
# Set the Bokeh model to use
_widget_type = _BkChartJS

# Rename Panel Parameters -> Bokeh Model properties
# Parameters like title that does not exist on the Bokeh model should be renamed to None
_rename: ClassVar[Mapping[str, str | None]] = {}

# Parameters to be mapped to Bokeh model properties
object = param.String(default="Click Me!")
clicks = param.Integer(default=0)

def _get_model(
self, doc: Document, root: Optional[Model] = None,
parent: Optional[Model] = None, comm: Optional[Comm] = None
) -> Model:
self._bokeh_model = lazy_load(
'panel.models.chartjs', 'ChartJS', isinstance(comm, JupyterComm), root
)
model = super()._get_model(doc, root, parent, comm)
self._register_events('chartjs_event', model=model, doc=doc, comm=comm)
return model
14 changes: 14 additions & 0 deletions panel/tests/pane/test_chartjs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import panel as pn


def test_constructor():
pn.pane.ChartJS(object="Click Me Now!")

def get_app():
chartjs = pn.pane.ChartJS(object="Click Me Now!", )
return pn.Column(
chartjs, pn.Param(chartjs, parameters=["object", "clicks"])
)

if __name__.startswith("bokeh"):
get_app().servable()
Loading