|
| 1 | +import random |
1 | 2 | from typing import Callable |
2 | 3 |
|
3 | 4 | from nicegui import ui |
4 | 5 |
|
5 | 6 | from ..style import subheading |
| 7 | +from .content.sub_pages_documentation import FakeSubPages |
6 | 8 | from .demo import demo |
7 | 9 |
|
8 | 10 |
|
9 | 11 | def create_intro() -> None: |
10 | | - @_main_page_demo('Styling', ''' |
11 | | - While having reasonable defaults, you can still modify the look of your app with CSS as well as Tailwind and Quasar classes. |
12 | | - ''') |
13 | | - def formatting_demo(): |
14 | | - ui.icon('thumb_up') |
15 | | - ui.markdown('This is **Markdown**.') |
16 | | - ui.html('This is <strong>HTML</strong>.', sanitize=False) |
17 | | - with ui.row(): |
18 | | - ui.label('CSS').style('color: #888; font-weight: bold') |
19 | | - ui.label('Tailwind').classes('font-serif') |
20 | | - ui.label('Quasar').classes('q-ml-xl') |
21 | | - ui.link('NiceGUI on GitHub', 'https://github.com/zauberzeug/nicegui') |
22 | | - |
23 | | - @_main_page_demo('Common UI Elements', ''' |
24 | | - NiceGUI comes with a collection of commonly used UI elements. |
| 12 | + @_main_page_demo('Single-Page Applications', ''' |
| 13 | + Build applications with fast client-side routing using [`ui.sub_pages`](/documentation/sub_pages) |
| 14 | + and a `root` function as single entry point. |
| 15 | + For each visitor, the `root` function is executed and generates the interface. |
| 16 | + [`ui.link`](/documentation/link) and [`ui.navigate`](/documentation/navigate) can be used to navigate to other sub pages. |
| 17 | +
|
| 18 | + If you do not want a single-page application, you can also use [`@ui.page('/your/path')`](/documentation/page) |
| 19 | + to define standalone content available at a specific path. |
25 | 20 | ''') |
26 | | - def common_elements_demo(): |
27 | | - from nicegui.events import ValueChangeEventArguments |
28 | | - |
29 | | - def show(event: ValueChangeEventArguments): |
30 | | - name = type(event.sender).__name__ |
31 | | - ui.notify(f'{name}: {event.value}') |
32 | | - |
33 | | - ui.button('Button', on_click=lambda: ui.notify('Click')) |
34 | | - with ui.row(): |
35 | | - ui.checkbox('Checkbox', on_change=show) |
36 | | - ui.switch('Switch', on_change=show) |
37 | | - ui.radio(['A', 'B', 'C'], value='A', on_change=show).props('inline') |
38 | | - with ui.row(): |
39 | | - ui.input('Text input', on_change=show) |
40 | | - ui.select(['One', 'Two'], value='One', on_change=show) |
41 | | - ui.link('And many more...', '/documentation').classes('mt-8') |
42 | | - |
43 | | - @_main_page_demo('Value Binding', ''' |
44 | | - Binding values between UI elements and data models is built into NiceGUI. |
| 21 | + def spa_demo(): |
| 22 | + sub_pages = None # HIDE |
| 23 | + |
| 24 | + def root(): |
| 25 | + # ui.sub_pages({ |
| 26 | + # '/': table_page, |
| 27 | + # '/map/{lat}/{lon}': map_page, |
| 28 | + # }).classes('w-full') |
| 29 | + nonlocal sub_pages # HIDE |
| 30 | + sub_pages = FakeSubPages({'/': table_page, '/map/{lat}/{lon}': map_page}).classes('w-full') # HIDE |
| 31 | + sub_pages.init() # HIDE |
| 32 | + |
| 33 | + def table_page(): |
| 34 | + ui.table(rows=[ |
| 35 | + {'name': 'New York', 'lat': 40.7119, 'lon': -74.0027}, |
| 36 | + {'name': 'London', 'lat': 51.5074, 'lon': -0.1278}, |
| 37 | + {'name': 'Tokio', 'lat': 35.6863, 'lon': 139.7722}, |
| 38 | + ]).on('row-click', lambda e: sub_pages._render('/map/{lat}/{lon}', lat=e.args[1]['lat'], lon=e.args[1]['lon'])) # HIDE |
| 39 | + # ]).on('row-click', lambda e: ui.navigate.to(f'/map/{e.args[1]["lat"]}/{e.args[1]["lon"]}')) |
| 40 | + |
| 41 | + def map_page(lat: float, lon: float): |
| 42 | + ui.leaflet(center=(lat, lon), zoom=10) |
| 43 | + # ui.link('Back to table', '/') |
| 44 | + sub_pages.link('Back to table', '/') # HIDE |
| 45 | + |
| 46 | + return root |
| 47 | + |
| 48 | + @_main_page_demo('Reactive Transformations', ''' |
| 49 | + Create real-time interfaces with automatic updates. |
| 50 | + Type and watch text flow in both directions. |
| 51 | + When input changes, the [binding](/documentation/section_binding_properties) transforms the text |
| 52 | + with a custom Python function and updates the label. |
45 | 53 | ''') |
46 | 54 | def binding_demo(): |
47 | | - class Demo: |
48 | | - def __init__(self): |
49 | | - self.number = 1 |
50 | | - |
51 | | - demo = Demo() |
52 | | - v = ui.checkbox('visible', value=True) |
53 | | - with ui.column().bind_visibility_from(v, 'value'): |
54 | | - ui.slider(min=1, max=3).bind_value(demo, 'number') |
55 | | - ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(demo, 'number') |
56 | | - ui.number().bind_value(demo, 'number') |
| 55 | + def root(): |
| 56 | + user_input = ui.input(value='Hello') |
| 57 | + ui.label().bind_text_from(user_input, 'value', reverse) |
| 58 | + |
| 59 | + def reverse(text: str) -> str: |
| 60 | + return text[::-1] |
| 61 | + |
| 62 | + return root |
| 63 | + |
| 64 | + @_main_page_demo('Event System', ''' |
| 65 | + Use an [Event](/documentation/event) to trigger actions and pass data. |
| 66 | + Here we have an IoT temperature sensor submitting its readings |
| 67 | + to a [FastAPI endpoint](/documentation/section_pages_routing#api_responses) with path "/sensor". |
| 68 | + When a new value arrives, it emits an event for the chart to be updated. |
| 69 | + ''') |
| 70 | + def event_system_demo(): |
| 71 | + import time |
| 72 | + |
| 73 | + from nicegui import Event, app |
| 74 | + |
| 75 | + sensor = Event[float]() |
| 76 | + |
| 77 | + # @app.post('/sensor') |
| 78 | + def sensor_webhook(temperature: float): |
| 79 | + sensor.emit(temperature) |
| 80 | + |
| 81 | + def root(): |
| 82 | + chart = ui.echart({ |
| 83 | + 'xAxis': {'type': 'time', 'axisLabel': {'hideOverlap': True}}, |
| 84 | + 'yAxis': {'type': 'value', 'min': 'dataMin'}, |
| 85 | + 'series': [{'type': 'line', 'data': [], 'smooth': True}], |
| 86 | + }) |
| 87 | + |
| 88 | + def update_chart(temperature: float): |
| 89 | + data = chart.options['series'][0]['data'] |
| 90 | + data.append([time.time(), temperature]) |
| 91 | + if len(data) > 10: |
| 92 | + data.pop(0) |
| 93 | + |
| 94 | + sensor.subscribe(update_chart) |
| 95 | + # END OF DEMO |
| 96 | + |
| 97 | + data = chart.options['series'][0]['data'] |
| 98 | + data.append([time.time(), 24.0]) |
| 99 | + ui.timer(1.0, lambda: update_chart(round(data[-1][1] + (random.random() - 0.5), 1)), immediate=False) |
| 100 | + app # noqa: B018 to avoid unused import warning |
| 101 | + |
| 102 | + return root |
57 | 103 |
|
58 | 104 |
|
59 | 105 | def _main_page_demo(title: str, explanation: str) -> Callable: |
|
0 commit comments