-
-
Notifications
You must be signed in to change notification settings - Fork 485
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
Try to fix Plotly layout issues #2957
Conversation
Even if the behavior is reverted to use Also I'm having trouble reproducing the initial height issue on Firefox and Chromium using the current nicegui development branch ( import plotly.graph_objects as go
from nicegui import ui
fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
ui.plotly(fig).classes('w-full h-40')
ui.run() |
As far as I know the issue doesn't occur when using The height issue seems to occur only if the creation of the Plotly element is slightly delayed: @ui.page('/test2')
def test2():
ui.timer(0.5, lambda: ui.plotly({}).classes('h-60'), once=True) |
One issue might be that the diff --git a/nicegui/elements/plotly.py b/nicegui/elements/plotly.py
index cc912581..5c4b7294 100644
--- a/nicegui/elements/plotly.py
+++ b/nicegui/elements/plotly.py
@@ -35,8 +35,8 @@ class Plotly(Element, component='plotly.vue', libraries=['lib/plotly/plotly.min.
super().__init__()
self.figure = figure
+ self.classes('js-plotly-plot')
self.update()
- self._classes.append('js-plotly-plot')
def update_figure(self, figure: Union[Dict, go.Figure]):
"""Overrides figure instance of this Plotly chart and updates chart on client side.""" ...actually now that I think about it I'm not sure why this fixes the issue. The |
Is there a race condition between the application of the classes (like |
I think the only way to control the initial height of the plotly plot in a stable way is to either
|
There is another race condition: after creating a div with class |
I see that tailwind is being used client-side as a script rather than a CSS static file - I think that explains the delay (the script is analyzing the page content to dynamically insert style rules?) It might be nice performance-wise to compile all the styles that would result from executing every combination in |
That's a very good point, @bmaranville. Quasar classes are generated via JavaScript, probably after the DOM has been created. But I still don't understand and would like to find out why the original plotly.vue (before PR #2917) now requires these changes in element.py. And why exactly We might end up setting an initial height somewhere via CSS or Plotly option. But it worked before PR #2917 and this PR fixes it again, so I'm not convinced that inducing another height value is necessary. I just don't understand and like the current solution, because it doesn't make much sense to me and it increases the payload again. |
The Quasar classes are not what I was referring to - it's the use of Tailwind as a script at runtime rather than using the Tailwind API to generate static CSS at build time. The use of the script version is not recommended for production in the Tailwind documentation: https://tailwindcss.com/docs/installation/play-cdn When using the Tailwind script there is automatically a race as an inline CSS is dynamically generated by a script that is reading the DOM and updating the CSS in real time, so after you add a class like Most of the time you don't care, because everything just updates pretty fast, but if you have a library that is trying to read the style of a container before rendering itself, it won't work. The times that it appears to work are probably because an additional I noticed that in the demo code for reproducing the problem, I still suggest that the only (stable) way to make sure the first render of the plot is sized to the container is to size the container before rendering. Sometimes other approaches will work because of race conditions. |
After an exciting debugging session with @codingpaula we finally seem to understand what is happening when we simply revert PR #2516:
We fixed it by replacing undefined element attributes before rendering anything. This not only avoids the buggy resizing of |
This PR tries to solve #2917 by reverting PR #2516 to avoid Plotly issue plotly/plotly.js#4856 (see /test1 below).
But reverting this PR doesn't seem to be enough, since demo plots on our documentation page still have a wrong height, which I reproduced with /test2 below. It turned out that we need to partly revert 328fbab as well to avoid this problem.
I'm still not very happy with this solution because it re-introduces unnecessary payload. So consider it as work in progress.