Skip to content

Changes to accomodate plotly v6 #182

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

Merged
merged 7 commits into from
Jan 29, 2025
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [UNRELEASED] - 2025-01-23

* Updates to accomodate the new plotly v6.0.0 release. (#182)
* Fixed an issue with plotly graphs sometimes not getting fully removed from the DOM. (#178)
* Fixed an issue with ipyleaflet erroring out when attempting to read the `.model_id` property of a closed widget object. (#179)
* Fixed an issue where altair charts would sometimes render to a 0 height after being shown, hidden, and then shown again. (#180)
Expand Down
21 changes: 12 additions & 9 deletions js/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ class OutputManager extends HTMLManager {
// Define our own custom module loader for Shiny
const shinyRequireLoader = async function(moduleName: string, moduleVersion: string): Promise<any> {

// shiny provides require.js and also sets `define.amd=false` to prevent <script>s
// with UMD loaders from triggering anonymous define() errors. shinywidgets should
// generally be able to avoid anonymous define errors though since there should only
// be one 'main' anonymous define() for the widget's module (located in a JS file that
// we've already require.config({paths: {...}})ed; and in that case, requirejs adds a
// data-requiremodule attribute to the <script> tag that shiny's custom define will
// recognize and use as the name).)
// shiny provides a shim of require.js which allows <script>s with anonymous
// define()s to be loaded without error. When an anonymous define() occurs,
// the shim uses the data-requiremodule attribute (set by require.js) on the script
// to determine the module name.
// https://github.com/posit-dev/py-shiny/blob/230940c/scripts/define-shims.js#L10-L16
// In the context of shinywidgets, when a widget gets rendered, it should
// come with another <script> tag that does `require.config({paths: {...}})`
// which maps the module name to a URL of the widget's JS file.
const oldAmd = (window as any).define.amd;

// The is the original value for define.amd that require.js sets
(window as any).define.amd = {jQuery: true};
// This is probably not necessary, but just in case -- especially now in a
// anywidget/ES6 world, we probably don't want to load AMD modules
// (plotly is one example of a widget that will fail to load if AMD is enabled)
(window as any).define.amd = false;

// Store jQuery global since loading we load a module, it may overwrite it
// (qgrid is one good example)
Expand Down
3 changes: 3 additions & 0 deletions shinywidgets/_render_widget_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]:
# so change that 60px default to 32px
if layout.margin["t"] == 60: # pyright: ignore
layout.margin["t"] = 32 # pyright: ignore
# In plotly >=v6.0, the plot won't actually fill unless it's responsive
if fill:
widget._config = {"responsive": True, **widget._config} # type: ignore

widget.layout = layout

Expand Down
48 changes: 33 additions & 15 deletions shinywidgets/_shinywidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ def _cleanup_session_state():

session.on_ended(_cleanup_session_state)

# Get the initial state of the widget
state, buffer_paths, buffers = _remove_buffers(w.get_state())

# Make sure window.require() calls made by 3rd party widgets
# (via handle_comm_open() -> new_model() -> loadClass() -> requireLoader())
# actually point to directories served locally by shiny
Expand All @@ -114,18 +111,39 @@ def _cleanup_session_state():

id = cast(str, w._model_id)

# Initialize the comm...this will also send the initial state of the widget
with widget_comm_patch():
w.comm = ShinyComm(
comm_id=id,
comm_manager=COMM_MANAGER,
target_name="jupyter.widgets",
data={"state": state, "buffer_paths": buffer_paths},
buffers=cast(BufferType, buffers),
# TODO: should this be hard-coded?
metadata={"version": __protocol_version__},
html_deps=session._process_ui(TagList(widget_dep))["deps"],
)
# Since the actual ShinyComm() is initialized _after_ the Widget is initialized,
# and Widget.__init__() includes a call to Widget.open() which opens an unnecessary
# comm, we just set the comm to a dummy comm for now (to avoid unnecessary work)
w.comm = OrphanedShinyComm(id)

# Schedule the opening of the comm to happen sometime after this init function.
# This is important for widgets like plotly that do additional initialization that
# is required to get a valid widget state.
@reactive.effect(priority=99999)
def _open_shiny_comm():

# Call _repr_mimebundle_() before get_state() since it may modify the widget
# in an important way (unfortunately, it does for plotly)
# # https://github.com/plotly/plotly.py/blob/0089f32/packages/python/plotly/plotly/basewidget.py#L734-L738
w._repr_mimebundle_()

# Now, get the state
state, buffer_paths, buffers = _remove_buffers(w.get_state())

# Initialize the comm -- this sends widget state to the frontend
with widget_comm_patch():
w.comm = ShinyComm(
comm_id=id,
comm_manager=COMM_MANAGER,
target_name="jupyter.widgets",
data={"state": state, "buffer_paths": buffer_paths},
buffers=cast(BufferType, buffers),
# TODO: should this be hard-coded?
metadata={"version": __protocol_version__},
html_deps=session._process_ui(TagList(widget_dep))["deps"],
)

_open_shiny_comm.destroy()

# If we're in a reactive context, close this widget when the context is invalidated
# TODO: this should probably only be done in an output context, but I'm pretty sure
Expand Down
2 changes: 1 addition & 1 deletion shinywidgets/static/output.js

Large diffs are not rendered by default.