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 4 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
2 changes: 1 addition & 1 deletion js/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const shinyRequireLoader = async function(moduleName: string, moduleVersion: str
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};
(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
43 changes: 28 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,34 @@ 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"],
)
# 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_comm():

# Call _repr_mimebundle_() before getting the state since it may modify the widget
# in an important way (unfortunately, plotly does this)
# # 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_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.