快速搭建web见面

Rodja Trappe 6caae91ea9 improved styling před 4 roky
.github fc127dc4fe trying to make automated pypi releases with poetry před 4 roky
.vscode 852faab6b1 do not set blank lines před 4 roky
nicegui 6caae91ea9 improved styling před 4 roky
sceenshots 02e770f4c1 adding docs for some interactive elements před 4 roky
.gitignore 11844e40cc cleanup před 4 roky
Dockerfile f0968beb00 adding markdown element and improving api_reference před 4 roky
LICENSE 0cb68ea25c Adding license před 4 roky
README.md 7b17c65555 better styling před 4 roky
api_reference.py 6caae91ea9 improved styling před 4 roky
docker-compose.yml 72d4c24e8b do not show reloading popups před 4 roky
docker.sh 627296fbd6 simplified docker.sh před 4 roky
main.py 1541486165 some design doc před 4 roky
nicegui.code-workspace e11050e6f3 made dev container debuggable před 4 roky
poetry.lock a49d8b5a42 introducing ui.html and nice presenting of docstrings in api_refrence před 4 roky
pyproject.toml a49d8b5a42 introducing ui.html and nice presenting of docstrings in api_refrence před 4 roky

README.md

NiceGUI

We like Streamlit but find it does to much magic when it comes to state handling. In search for an alernative nice library to write simple graphical user interfaces in Python we discovered justpy. While too "low-level-html" for our daily usage it provides a great basis for our shot at a "NiceGUI".

Features

  • browser-based GUI
  • implicit reload on code change
  • clean set of GUI elements (label, button, checkbox, switch, slider, input, ...)
  • simple grouping with rows, columns and cards
  • built-in timer to refresh data in intervals (even every 10 ms)
  • straight-forward data bindings to write even less code

Install

python3 -m pip install nicegui

Usage

Write your nice GUI in a file main.py:

from nicegui import ui

ui.label('Hello NiceGUI!')
ui.button('BUTTON', on_click=lambda: print('button was pressed'))

Launch it with:

python3 main.py

Note: The script will automatically reload the GUI if you modify your code.

API

See main.py for an extensive example what you can do with NiceGUI.

Interactive Elements

Button, Checkbox and Switch require a name which is displayed as their label and a callback. The callback can have an optional event parameter which provides informations like sender and value:

ui.button('Button', on_click=lambda: result.set_text('Button: pressed'))
ui.checkbox('Checkbox', on_change=lambda e: result.set_text(f'checkbox: {e.value}'))
ui.switch('Switch', on_change=lambda e: result.set_text(f'switch: {e.value}'))

result = ui.label('please interact', typography='bold')

Use ui.input to receive text and ui.number for explicit number input.

ui.input(label='Text', on_change=lambda e: result.set_text(e.value))
ui.number(label='Number', format='%.2f', on_change=lambda e: result.set_text(e.value))

result = ui.label('please type', typography='bold')

Pre-fill ui.input with the text property and ui.number with value.

Styling & Design

NiceGUI use the Quasar Framework and hence has their full design power. Each NiceGUI element provides a design property which content is passed as props the Quasar component:

ui.radio(['x', 'y', 'z'], design='inline color=green')
ui.button(icon='touch_app', design='outline round')

Have a look at the Quasar documentation for all styling "props".

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 (see plots below):

clock = ui.label()
ui.timer(interval=0.1, callback=lambda: clock.set_text(datetime.now().strftime("%X")))

With an optional third parameter once=True the callback is once executed after an delay specified by interval. Otherwise the callback is run repeatedly.

Plots

To render a simple plot you create a new context and call the neccessary Matplotlib functions:

from nicegui import ui
from matplotlib import pyplot as plt
import numpy as np

with ui.plot():
    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')

To update a plot in regular intervals, have look at main.py.

To simplify live updating line plots even more, NiceGUI provides ui.line_plot with useful parameters and a push method:

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()],
]))