Kaynağa Gözat

introduce pyserial example

Falko Schindler 1 yıl önce
ebeveyn
işleme
62f477fbb8

+ 23 - 0
examples/pyserial/main.py

@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+import serial
+
+from nicegui import app, run, ui
+
+port = serial.Serial('/dev/tty.SLAB_USBtoUART', baudrate=115200, timeout=0.1)
+
+ui.input('Send command').on('keydown.enter', lambda e: (
+    port.write(f'{e.sender.value}\n'.encode()),
+    e.sender.set_value(''),
+))
+log = ui.log()
+
+
+async def read_loop() -> None:
+    while not app.is_stopped:
+        line = await run.io_bound(port.readline)
+        if line:
+            log.push(line.decode())
+
+app.on_startup(read_loop)
+
+ui.run()

+ 1 - 0
examples/pyserial/requirements.txt

@@ -0,0 +1 @@
+pyserial

+ 5 - 0
test_startup.sh

@@ -42,6 +42,11 @@ do
         continue # until https://github.com/omnilib/aiosqlite/issues/241 is fixed
     fi
 
+    # skip if path is examples/pyserial
+    if test $path = "examples/pyserial"; then
+        continue # because there is no serial port in github actions
+    fi
+
     # install all requirements except nicegui
     if test -f $path/requirements.txt; then
         sed '/^nicegui/d' $path/requirements.txt > $path/requirements.tmp.txt || error=1 # remove nicegui from requirements.txt

+ 4 - 3
website/examples.py

@@ -36,7 +36,8 @@ examples: List[Example] = [
     Example('NGINX Subpath', 'shows the setup to serve an app behind a reverse proxy subpath'),
     Example('Script Executor', 'executes scripts on selection and displays the output'),
     Example('Local File Picker', 'demonstrates a dialog for selecting files locally on the server'),
-    Example('Search as you type', 'using public API of thecocktaildb.com to search for cocktails'),
+    Example('Search as you type',
+            'using public API of [thecocktaildb.com](https://www.thecocktaildb.com/) to search for cocktails'),
     Example('Menu and Tabs', 'uses Quasar to create foldable menu and tabs inside a header bar'),
     Example('Todo list', 'shows a simple todo list with checkboxes and text input'),
     Example('Trello Cards', 'shows Trello-like cards that can be dragged and dropped into columns'),
@@ -57,7 +58,7 @@ examples: List[Example] = [
     Example('Descope Auth', 'login form and user profile using [Descope](https://descope.com)'),
     Example('Editable table', 'editable table allowing to add, edit, delete rows'),
     Example('Editable AG Grid', 'editable AG Grid allowing to add, edit, delete rows'),
-    Example('FullCalendar',
-            'An interactive calendar display using the [FullCalendar library](https://fullcalendar.io/)'),
+    Example('FullCalendar', 'show an interactive calendar using the [FullCalendar library](https://fullcalendar.io/)'),
     Example('Pytest', 'test a NiceGUI app with pytest'),
+    Example('Pyserial', 'communicate with a serial device'),
 ]