-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Switch to use anywidget #4823
Switch to use anywidget #4823
Conversation
FYI @marthacryan when I install from the build files in CI nothing renders. Does |
@ndrezn Which CI artifact are you using? And could you give me more info on the steps you took to install? I'm not able to reproduce |
@marthacryan I followed your steps for testing locally and the
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
At least one more reviewer would be ideal since I'm not a frequent Jupyter Notebook user, but looks great to me.
|
||
def display_jupyter_version_warnings(): | ||
|
||
parent_process = psutil.Process().parent().cmdline()[-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK how this would happen in practice, but in principle .parent()
can return None which would make this fail. In that case I guess we aren't in a jupyter context so we can short-circuit the rest of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some code to address this! Let me know if you think there's a better approach than a try/except
|
||
parent_process = psutil.Process().parent().cmdline()[-1] | ||
|
||
if "jupyter-notebook" in parent_process and jupyter_notebook.__version__ < "7": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the optional_imports.get_module
calls inside these conditionals? Something like:
if "jupyter-notebook" in parent_process:
jupyter_notebook = optional_imports.get_module("notebook")
if jupyter_notebook.__version__ < "7":
and similar for jupyterlab
. Those imports can be slow (particularly jupyterlab
, it's nearly a second on my computer), no need to increase everyone's startup time (for example Dash apps). And calling optional_imports.get_module
on an already-loaded module or a nonexistent module you've previously asked for is very fast.
I wonder about the existing optional import calls in _renderers.py
as well - looks like IPython
and IPython.display
are automatically imported when you're in an IPython or jupyter environment, so I bet we could just add should_load=False
to those calls. nbformat
is not automatically loaded, so it would need to move into the show
method where it's called, like I'm suggesting here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I moved them into the if statements. I'm not sure about the other optional calls in that file but I can make an issue to investigate that more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just made this issue: #4856
Just had a chance to read the blog post, congrats on the pre-release! I have a quick question about this part:
This language suggests this incompatibility is due to anywidget. However, anywidget should be compatible with at least Jupyter Notebook v6, so just wanted to clarify. Is there some API that is missing that the Steps to reproduce with uv (via juv). uvx juv init example.ipynb
uvx juv add example.ipynb anywidget==0.9.13 plotly==6.0.0rc0
uvx juv run --jupyter=notebook@6 example.ipynb import plotly.graph_objects as go
f = go.FigureWidget()
f
# Warning: Plotly version >= 6 requires Jupyter Notebook >= 7 but you have 6.0.0 installed. import anywidget
import traitlets
class CounterWidget(anywidget.AnyWidget):
_esm = """
function render({ model, el }) {
let count = () => model.get("value");
let btn = document.createElement("button");
btn.innerHTML = `count is ${count()}`;
btn.addEventListener("click", () => {
model.set("value", count() + 1);
model.save_changes();
});
model.on("change:value", () => {
btn.innerHTML = `count is ${count()}`;
});
el.appendChild(btn);
}
export default { render };
"""
value = traitlets.Int(0).tag(sync=True)
CounterWidget() # shows button |
Hey @manzt, that's a mistake in the blog post. We're dropping support for <7.0 primarily because it simplifies our build and release process; it is unrelated to AnyWidget. I'll make sure that gets corrected -- thanks for taking the time to deep dive on this & apologies for that error! |
Co-authored by @manzt! This updates the logic for the
FigureWidget
to rely on anywidget.A quote about anywidget from the documentation for context:
How to test
Create a python environment.
Docs on how to use
FigureWidget
: https://plotly.com/python/figurewidget/More context
This will not affect the
go.Figure
class. High level explanation of the difference between usinggo.Figure
andgo.FigureWidget
:go.Figure
: The chart generated by this object will be interactive on the client side. For many purposes, this is all a user needs to do with a chart. The interactions are things like zooming in, or any of the various operations in the toolbar.go.FigureWidget
: The difference here is that the figure is interactive on the server side as well.Some info about the code changes:
ipywidgets
toanywidget
. The majority of the files changed in this PR are only changing that.packages/python/plotly
calledjs
, which contains:package.json
: A very small amount of dependencies and scripts to buildbundle.js
widget.ts
: The code used by jupyter to render and interact with the widget. This is transpiled intopackages/python/plotly/plotly/bundle.js
and referenced frombasewidget.py
.tsconfig.json
: Configurations for transpilingwidget.ts
.