瀏覽代碼

moving line_plot from readme to main.py

Rodja Trappe 4 年之前
父節點
當前提交
73ad711fee
共有 5 個文件被更改,包括 26 次插入22 次删除
  1. 0 16
      README.md
  2. 9 1
      main.py
  3. 12 2
      nicegui/elements/line_plot.py
  4. 2 1
      nicegui/elements/plot.py
  5. 3 2
      nicegui/timer.py

+ 0 - 16
README.md

@@ -47,19 +47,3 @@ Note: The script will automatically reload the GUI if you modify your code.
 ## API
 
 API Reference is hosted at [https://nicegui.io](https://nicegui.io). Also have a look at [examples.py](https://github.com/zauberzeug/nicegui/tree/main/examples.py) for an extensive demonstration what you can do with NiceGUI.
-
-### Plots
-
-To update a plot in regular intervals, have look at [main.py](https://github.com/zauberzeug/nicegui/tree/main/main.py).
-
-<img src="https://raw.githubusercontent.com/zauberzeug/nicegui/main/sceenshots/demo-live-plot.gif" width="300" align="right">
-
-To simplify live updating line plots even more, NiceGUI provides `ui.line_plot` with useful parameters and a `push` method:
-
-```python
-lines = ui.line_plot(n=2, limit=20).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
-ui.timer(0.1, lambda: lines.push([datetime.now()], [
-    [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
-    [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
-]))
-```

+ 9 - 1
main.py

@@ -95,9 +95,17 @@ with example(ui.plot):
     from matplotlib import pyplot as plt
     import numpy as np
 
-    with ui.plot(figsize=(2.5, 1.6)):
+    with ui.plot(figsize=(2.5, 1.8)):
         x = np.linspace(0.0, 5.0)
         y = np.cos(2 * np.pi * x) * np.exp(-x)
         plt.plot(x, y, '-')
         plt.xlabel('time (s)')
         plt.ylabel('Damped oscillation')
+
+with example(ui.line_plot):
+    lines = ui.line_plot(n=2, limit=20, figsize=(2.5, 1.8)).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
+    line_updates = ui.timer(0.1, lambda: lines.push([datetime.now()], [
+        [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
+        [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
+    ]), active=False)
+    ui.checkbox('active').bind_value(line_updates.active)

+ 12 - 2
nicegui/elements/line_plot.py

@@ -3,9 +3,19 @@ from .plot import Plot
 
 class LinePlot(Plot):
 
-    def __init__(self, n: int = 1, limit: int = 100, update_every=1, close: bool = True):
+    def __init__(self, n: int = 1, limit: int = 100, update_every=1, close: bool = True, **kwargs):
+        """Plot
 
-        super().__init__(close)
+        Create a context to configure a simple line plot. 
+        The  `push` method simplifies live updating.
+
+        :param n: number of data points to begin with
+        :param limit: maximum number of datapoints (new ones will push out the oldest)
+        :param close: weather the figure should be closed after exiting the context; set to False if you want to update it later, default is True
+        :param kwargs: arguments like `figsize` which should be passed to `pyplot.figure <https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html>`_
+        """
+
+        super().__init__(close, **kwargs)
 
         self.x = []
         self.Y = [[] for _ in range(n)]

+ 2 - 1
nicegui/elements/plot.py

@@ -8,9 +8,10 @@ class Plot(Element):
     def __init__(self, close: bool = True, **kwargs):
         """Plot
 
-        Create a context to configure a `Matplotlib <https://matplotlib.org/>`_ plot
+        Create a context to configure a `Matplotlib <https://matplotlib.org/>`_ plot.
 
         :param close: weather the figure should be closed after exiting the context; set to False if you want to update it later, default is True
+        :param kwargs: arguments like `figsize` which should be passed to `pyplot.figure <https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html>`_
         """
 
         self.close = close

+ 3 - 2
nicegui/timer.py

@@ -11,18 +11,19 @@ class Timer:
 
     active = BindableProperty
 
-    def __init__(self, interval, callback, *, once=False):
+    def __init__(self, interval, callback, *, active=True, once=False):
         """Timer
 
         One major drive behind the creation of NiceGUI was the necessity to have an simple approach to update the interface in regular intervals. For example to show a graph with incomming measurements.
 
         :param interval: the interval in which the timer is been called
         :param callback: function to execute when interval elapses
+        :param active: weather timer should run or be paused
         :param once: weather the callback is only executed once after an delay specified by `interval`; default is False
         """
 
         parent = Element.view_stack[-1]
-        self.active = True
+        self.active = active
 
         async def timeout():