plotly_documentation.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.plotly)
  4. def main_demo() -> None:
  5. import plotly.graph_objects as go
  6. fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))
  7. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  8. ui.plotly(fig).classes('w-full h-40')
  9. @doc.demo('Dictionary interface', '''
  10. This demo shows how to use the declarative dictionary interface to create a plot.
  11. For plots with many traces and data points, this is more efficient than the object-oriented interface.
  12. The definition corresponds to the [JavaScript Plotly API](https://plotly.com/javascript/).
  13. Due to different defaults, the resulting plot may look slightly different from the same plot created with the object-oriented interface,
  14. but the functionality is the same.
  15. ''')
  16. def plot_dict_interface():
  17. fig = {
  18. 'data': [
  19. {
  20. 'type': 'scatter',
  21. 'name': 'Trace 1',
  22. 'x': [1, 2, 3, 4],
  23. 'y': [1, 2, 3, 2.5],
  24. },
  25. {
  26. 'type': 'scatter',
  27. 'name': 'Trace 2',
  28. 'x': [1, 2, 3, 4],
  29. 'y': [1.4, 1.8, 3.8, 3.2],
  30. 'line': {'dash': 'dot', 'width': 3},
  31. },
  32. ],
  33. 'layout': {
  34. 'margin': {'l': 15, 'r': 0, 't': 0, 'b': 15},
  35. 'plot_bgcolor': '#E5ECF6',
  36. 'xaxis': {'gridcolor': 'white'},
  37. 'yaxis': {'gridcolor': 'white'},
  38. },
  39. }
  40. ui.plotly(fig).classes('w-full h-40')
  41. @doc.demo('Plot updates', '''
  42. This demo shows how to update the plot in real time.
  43. Click the button to add a new trace to the plot.
  44. To send the new plot to the browser, make sure to explicitly call `plot.update()` or `ui.update(plot)`.
  45. ''')
  46. def plot_updates():
  47. from random import random
  48. import plotly.graph_objects as go
  49. fig = go.Figure()
  50. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  51. plot = ui.plotly(fig).classes('w-full h-40')
  52. def add_trace():
  53. fig.add_trace(go.Scatter(x=[1, 2, 3], y=[random(), random(), random()]))
  54. plot.update()
  55. ui.button('Add trace', on_click=add_trace)
  56. doc.reference(ui.plotly)