-
-
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
init_notebook_mode(connected=False) and pio.renderers.default='notebook' should not include full plotly.js on each call, but should check if already loaded #1531
Comments
Hi @ociule, Thanks for the report. This behavior should be possible with the new renderers subsystem that was recently merged in 3.8.0. See #1474 for more info. Note this is still considered somewhat experimental and isn't documented yet. That will wait until version 4.0. Basically, you would set the default renderer to Something like import plotly.graph_objs as go
import plotly.io as pio
pio.renderers.default = 'notebook'
def my_thing():
xs = [0, 1, 2, 3, 4]
ys = [4, 3, 2, 1, 0]
trace = go.Scatter(
x = xs,
y = ys,
mode = 'markers'
)
data = [trace]
fig = go.Figure(data=data)
pio.show(fig)
# First call causes plotly.js to be loaded in the notebook
my_thing()
# Subsequent calls don't load plotly.js again
my_thing() The nice thing about this approach is that you can completely change rendering context without modifying Does this work for your usecase? |
This looks really good, testing it soon. I'm only concerned about how the decision to load plotly.js or not is made when pio.show is called. If it's made in Python, not js, then it can't be aware of the state of the js environment. That's necessary for my usecase, unfortunately: people tend to keep a running jupyter notebook that they reopen, which does not reload the js. |
This is fantastic, and works as expected. It reads js env state to check if plotly.js is already loaded, so calling pio.show reliably loads plotly.js on the first call in a new browser tab (new js env), even if the notebook was running (old python env). This is great news and covers my use case. |
After further testing, I have to correct my previous comment: it does not seem to check js env state. Here's a conclusive test:
When executing the two cells sequentially, this works as expected. Afaik I can investigate through my tests, Moving It would be a much better solution if calling |
To help others, to fix my above issue temporarily, I'm using |
My tests were on plotly 3.9.0, btw. |
Hi @ociule, Yeah, the only difference between You're right that it's not really possible, as far as I'm aware, to check whether plotly.js is loaded into the browser from Python. In addition to the
|
Hi - we are currently trying to tidy up Plotly's public repositories to help us focus our efforts on things that will help users most. Since this issue has been sitting for several years, I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our backlog. Thanks for your help - @gvwilson |
The current js generated by init_notebook_mode(connected=False) is naive: it includes a require.undef("plotly") to remove plotly and the full plotlyjs code to create it again. This adds 6MB for each call to init_notebook_mode to the notebook size. It also makes opening the notebook slower.
Including it once should be enough.
Now just call iplot_my_thing twice, in two different jupyter cells. Save the notebook after each call and check its size.
The code shown wraps init_notebook_mode and iplot. This is necessary so calls to iplot_my_thing work under all circumstances, including users that load a notebook without restarting the kernel and not running the cell containing init_notebook_mode.
If this js fix is implemented, plotly.offline.iplot could just call init_notebook_mode directly and init_notebook_mode would not be needed in the public plotly API which would be a better UX.
The text was updated successfully, but these errors were encountered: