Skip to content

Commit c2b6d35

Browse files
authored
Add bokeh_dependency() to make it easier to render bokeh widgets (#85)
1 parent 2b9bd2d commit c2b6d35

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to shinywidgets will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
* Closed #14: Added a `bokeh_dependency()` function to simplify use of bokeh widgets. (#85)
11+
812
## [0.1.6] - 2023-03-24
913

1014
* Closed #79: make shinywidgets compatible with ipywidgets 8.0.5. (#66)

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,14 @@ To use `{bokeh}` in notebook, you have to run `bokeh.io.output_notebook()`. The
235235
equivalent thing in Shiny is to include the following in the UI definition:
236236

237237
```py
238-
from bokeh.resources import Resources
239-
head_content(HTML(Resources(mode="inline").render()))
238+
from shiny import ui
239+
from shinywidgets import bokeh_dependencies
240+
241+
app_ui = ui.page_fluid(
242+
bokeh_dependencies(),
243+
# ...
244+
)
245+
```
240246
```
241247
#### Other widgets?
242248

examples/outputs/app.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
import warnings
2-
31
import numpy as np
42
import pandas as pd
5-
from htmltools import HTML, head_content
63
from shiny import *
74

85
from shinywidgets import *
96

10-
# TODO: jupyter_bokeh assumes this additional JS has been loaded into the
11-
# (in a notebook, this comes in via bokeh.io.output_notebook()).
12-
# We could probably insert this on widget construction, just before the comm
13-
# gets initialized (it's not currently working due to run_coro_sync() not working
14-
# when session._send_message() wants to send a big payload).
15-
bokeh_dependency = None
16-
try:
17-
from bokeh.resources import Resources
18-
19-
bokeh_dependency = head_content(HTML(Resources(mode="inline").render()))
20-
except ImportError:
21-
warnings.warn("Could not import bokeh", stacklevel=2)
22-
23-
247
app_ui = ui.page_fluid(
25-
bokeh_dependency,
8+
bokeh_dependency(),
269
ui.layout_sidebar(
2710
ui.panel_sidebar(
2811
ui.input_radio_buttons(

shinywidgets/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
__author__ = """Carson Sievert"""
44
__email__ = "carson@rstudio.com"
5-
__version__ = "0.1.6"
5+
__version__ = "0.1.6.9000"
66

7+
from ._dependencies import bokeh_dependency
78
from ._shinywidgets import output_widget, reactive_read, register_widget, render_widget
89

9-
__all__ = ("output_widget", "register_widget", "render_widget", "reactive_read")
10+
__all__ = ("output_widget", "register_widget", "render_widget", "reactive_read", "bokeh_dependency")

shinywidgets/_dependencies.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ipywidgets.widgets.domwidget import DOMWidget
1717
from ipywidgets.widgets.widget import Widget
1818
from jupyter_core.paths import jupyter_path # type: ignore
19-
from shiny import Session
19+
from shiny import Session, ui
2020

2121
from . import __version__
2222

@@ -112,6 +112,13 @@ def require_dependency(w: Widget, session: Session) -> Optional[HTMLDependency]:
112112
)
113113

114114

115+
def bokeh_dependency() -> HTMLDependency:
116+
from bokeh.resources import Resources
117+
118+
resources = Resources(mode="inline").render()
119+
return ui.head_content(ui.HTML(resources))
120+
121+
115122
def jupyter_extension_path(module_name: str) -> Optional[str]:
116123
paths: List[str] = jupyter_path()
117124
module_dir = None

0 commit comments

Comments
 (0)