1
0

overview.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. from nicegui import ui
  2. from . import (
  3. doc,
  4. section_action_events,
  5. section_audiovisual_elements,
  6. section_binding_properties,
  7. section_configuration_deployment,
  8. section_controls,
  9. section_data_elements,
  10. section_page_layout,
  11. section_pages_routing,
  12. section_styling_appearance,
  13. section_testing,
  14. section_text_elements,
  15. )
  16. from ...style import subheading
  17. doc.title('*NiceGUI* Documentation', 'Reference, Demos and more')
  18. doc.text('Overview', '''
  19. NiceGUI is an open-source Python library to write graphical user interfaces which run in the browser.
  20. It has a very gentle learning curve while still offering the option for advanced customizations.
  21. NiceGUI follows a backend-first philosophy:
  22. It handles all the web development details.
  23. You can focus on writing Python code.
  24. This makes it ideal for a wide range of projects including short
  25. scripts, dashboards, robotics projects, IoT solutions, smart home automation, and machine learning.
  26. ''')
  27. doc.text('How to use this guide', '''
  28. This documentation explains how to use NiceGUI.
  29. Each of the tiles covers a NiceGUI topic in detail.
  30. It is recommended to start by reading this entire introduction page, then refer to other sections as needed.
  31. ''')
  32. doc.text('Basic concepts', '''
  33. NiceGUI provides UI _elements_ such as buttons, sliders, text, images, charts, and more.
  34. Your app assembles these components into _pages_.
  35. When the user interacts with an item on a page, NiceGUI triggers an _event_ (or _action_).
  36. You define code to _handle_ each event, such as what to do when a user clicks a button, modifies a value or operates a slider.
  37. Elements can also be bound to a _model_ (data object), which automatically updates the user interface when the model value changes.
  38. Elements are arranged on a page using a "declarative UI" or "code-based UI".
  39. That means that you also write structures like grids, cards, tabs, carousels, expansions, menus, and other layout elements directly in code.
  40. This concept has been made popular with Flutter and SwiftUI.
  41. For readability, NiceGUI utilizes Python's `with ...` statement.
  42. This context manager provides a nice way to indent the code to resemble the layout of the UI.
  43. Styling and appearance can be controlled in several ways.
  44. Most elements accept optional arguments for common styling and behavior changes, such as button icons or text color.
  45. Because NiceGUI is a web framework, you can change almost any appearance of an element with CSS.
  46. But elements also provide `.classes` and `.props` methods to apply Tailwind CSS and Quasar properties
  47. which are more high-level and simpler to use day-to-day after you get the hang of it.
  48. ''')
  49. doc.text('Actions, Events and Tasks', '''
  50. NiceGUI uses an async/await event loop for concurrency which is resource-efficient and has the great benefit of not having to worry about thread safety.
  51. This section shows how to handle user input and other events like timers and keyboard bindings.
  52. It also describes helper functions to wrap long-running tasks in asynchronous functions to keep the UI responsive.
  53. Keep in mind that all UI updates must happen on the main thread with its event loop.
  54. ''')
  55. doc.text('Implementation', '''
  56. NiceGUI is implemented with HTML components served by an HTTP server (FastAPI), even for native windows.
  57. If you already know HTML, everything will feel very familiar.
  58. If you don't know HTML, that's fine too!
  59. NiceGUI abstracts away the details, so you can focus on creating beautiful interfaces without worrying about how they are implemented.
  60. ''')
  61. doc.text('Running NiceGUI Apps', '''
  62. There are several options for deploying NiceGUI.
  63. By default, NiceGUI runs a server on localhost and runs your app as a private web page on the local machine.
  64. When run this way, your app appears in a web browser window.
  65. You can also run NiceGUI in a native window separate from a web browser.
  66. Or you can run NiceGUI on a server that handles many clients - the website you're reading right now is served from NiceGUI.
  67. After creating your app pages with components, you call `ui.run()` to start the NiceGUI server.
  68. Optional parameters to `ui.run` set things like the network address and port the server binds to,
  69. whether the app runs in native mode, initial window size, and many other options.
  70. The section _Configuration and Deployment_ covers the options to the `ui.run()` function and the FastAPI framework it is based on.
  71. ''')
  72. doc.text('Customization', '''
  73. If you want more customization in your app, you can use the underlying Tailwind classes and Quasar components
  74. to control the style or behavior of your components.
  75. You can also extend the available components by subclassing existing NiceGUI components or importing new ones from Quasar.
  76. All of this is optional.
  77. Out of the box, NiceGUI provides everything you need to make modern, stylish, responsive user interfaces.
  78. ''')
  79. doc.text('Testing', '''
  80. NiceGUI provides a comprehensive testing framework based on [pytest](https://docs.pytest.org/)
  81. which allows you to automate the testing of your user interface.
  82. You can utilize the `screen` fixture which starts a real (headless) browser to interact with your application.
  83. This is great if you have browser-specific behavior to test.
  84. But most of the time, NiceGUI's newly introduced `user` fixture is more suited:
  85. It only simulates the user interaction on a Python level and, hence, is blazing fast.
  86. That way the classical [test pyramid](https://martinfowler.com/bliki/TestPyramid.html),
  87. where UI tests are considered slow and expensive, does not apply anymore.
  88. This can have a huge impact on your development speed, quality and confidence.
  89. ''')
  90. tiles = [
  91. (section_text_elements, '''
  92. Elements like `ui.label`, `ui.markdown`, `ui.restructured_text` and `ui.html` can be used to display text and other content.
  93. '''),
  94. (section_controls, '''
  95. NiceGUI provides a variety of elements for user interaction, e.g. `ui.button`, `ui.slider`, `ui.inputs`, etc.
  96. '''),
  97. (section_audiovisual_elements, '''
  98. You can use elements like `ui.image`, `ui.audio`, `ui.video`, etc. to display audiovisual content.
  99. '''),
  100. (section_data_elements, '''
  101. There are several elements for displaying data, e.g. `ui.table`, `ui.aggrid`, `ui.highchart`, `ui.echart`, etc.
  102. '''),
  103. (section_binding_properties, '''
  104. To update UI elements automatically, you can bind them to each other or to your data model.
  105. '''),
  106. (section_page_layout, '''
  107. This section covers fundamental techniques as well as several elements to structure your UI.
  108. '''),
  109. (section_styling_appearance, '''
  110. NiceGUI allows to customize the appearance of UI elements in various ways, including CSS, Tailwind CSS and Quasar properties.
  111. '''),
  112. (section_action_events, '''
  113. This section covers timers, UI events, and the lifecycle of NiceGUI apps.
  114. '''),
  115. (section_pages_routing, '''
  116. A NiceGUI app can consist of multiple pages and other FastAPI endpoints.
  117. '''),
  118. (section_configuration_deployment, '''
  119. Whether you want to run your app locally or on a server, native or in a browser, we got you covered.
  120. '''),
  121. (section_testing, '''
  122. Write automated UI tests which run in a headless browser (slow) or fully simulated in Python (fast).
  123. '''),
  124. ]
  125. @doc.extra_column
  126. def create_tiles():
  127. with ui.row().classes('items-center content-between'):
  128. ui.label('If you like NiceGUI, go and become a')
  129. ui.html('<iframe src="https://github.com/sponsors/zauberzeug/button" title="Sponsor zauberzeug" height="32" width="114" style="border: 0; border-radius: 6px;"></iframe>')
  130. for documentation, description in tiles:
  131. page = doc.get_page(documentation)
  132. with ui.link(target=f'/documentation/{page.name}') \
  133. .classes('bg-[#5898d420] p-4 self-stretch rounded flex flex-col gap-2') \
  134. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  135. if page.title:
  136. ui.label(page.title.replace('*', '')).classes(replace='text-2xl')
  137. ui.markdown(description).classes(replace='bold-links arrow-links')
  138. @doc.ui
  139. def map_of_nicegui():
  140. ui.separator().classes('mt-6')
  141. subheading('Map of NiceGUI', anchor_name='map-of-nicegui')
  142. ui.add_css('''
  143. .map-of-nicegui a code {
  144. font-weight: bold;
  145. }
  146. ''')
  147. ui.markdown('''
  148. This overview shows the structure of NiceGUI.
  149. It is a map of the NiceGUI namespace and its contents.
  150. It is not exhaustive, but it gives you a good idea of what is available.
  151. An ongoing goal is to make this map more complete and to add missing links to the documentation.
  152. #### `ui`
  153. UI elements and other essentials to run a NiceGUI app.
  154. - [`ui.element`](/documentation/element): base class for all UI elements
  155. - customization:
  156. - `.props()` and [`.default_props()`](/documentation/element#default_props): add Quasar props and regular HTML attributes
  157. - `.classes()` and [`.default_classes()`](/documentation/element#default_classes): add Quasar, Tailwind and custom HTML classes
  158. - [`.tailwind`](/documentation/section_styling_appearance#tailwind_css): convenience API for adding Tailwind classes
  159. - `.style()` and [`.default_style()`](/documentation/element#default_style): add CSS style definitions
  160. - [`.tooltip()`](/documentation/tooltip): add a tooltip to an element
  161. - [`.mark()`](/documentation/element_filter#markers): mark an element for querying with an [ElementFilter](/documentation/element_filter)
  162. - interaction:
  163. - [`.on()`](/documentation/generic_events): add Python and JavaScript event handlers
  164. - `.update()`: send an update to the client (mostly done automatically)
  165. - `.run_method()`: run a method on the client side
  166. - `.get_computed_prop()`: get the value of a property that is computed on the client side
  167. - hierarchy:
  168. - `with ...:` nesting elements in a declarative way
  169. - `__iter__`: an iterator over all child elements
  170. - `ancestors`: an iterator over the element's parent, grandparent, etc.
  171. - `descendants`: an iterator over all child elements, grandchildren, etc.
  172. - `slots`: a dictionary of named slots
  173. - `add_slot`: fill a new slot with NiceGUI elements or a scoped slot with template strings
  174. - [`clear`](/documentation/section_page_layout#clear_containers): remove all child elements
  175. - [`move`](/documentation/element#move_elements): move an element to a new parent
  176. - `remove`: remove a child element
  177. - `delete`: delete an element and all its children
  178. - `is_deleted`: whether an element has been deleted
  179. - elements:
  180. - [`ui.aggrid`](/documentation/aggrid)
  181. - [`ui.audio`](/documentation/audio)
  182. - [`ui.avatar`](/documentation/avatar)
  183. - [`ui.badge`](/documentation/badge)
  184. - [`ui.button`](/documentation/button)
  185. - [`ui.button_group`](/documentation/button_group)
  186. - [`ui.card`](/documentation/card), `ui.card_actions`, `ui.card_section`
  187. - [`ui.carousel`](/documentation/carousel), `ui.carousel_slide`
  188. - [`ui.chat_message`](/documentation/chat_message)
  189. - [`ui.checkbox`](/documentation/checkbox)
  190. - [`ui.chip`](/documentation/chip)
  191. - [`ui.circular_progress`](/documentation/circular_progress)
  192. - [`ui.code`](/documentation/code)
  193. - [`ui.codemirror`](/documentation/codemirror)
  194. - [`ui.color_input`](/documentation/color_input)
  195. - [`ui.color_picker`](/documentation/color_picker)
  196. - [`ui.column`](/documentation/column)
  197. - [`ui.context_menu`](/documentation/context_menu)
  198. - [`ui.date`](/documentation/date)
  199. - [`ui.dialog`](/documentation/dialog)
  200. - [`ui.dropdown_button`](/documentation/button_dropdown)
  201. - [`ui.echart`](/documentation/echart)
  202. - [`ui.editor`](/documentation/editor)
  203. - [`ui.expansion`](/documentation/expansion)
  204. - [`ui.grid`](/documentation/grid)
  205. - [`ui.highchart`](/documentation/highchart)
  206. - [`ui.html`](/documentation/html)
  207. - [`ui.icon`](/documentation/icon)
  208. - [`ui.image`](/documentation/image)
  209. - [`ui.input`](/documentation/input)
  210. - [`ui.interactive_image`](/documentation/interactive_image)
  211. - `ui.item`, `ui.item_label`, `ui.item_section`
  212. - [`ui.joystick`](/documentation/joystick)
  213. - [`ui.json_editor`](/documentation/json_editor)
  214. - [`ui.knob`](/documentation/knob)
  215. - [`ui.label`](/documentation/label)
  216. - [`ui.leaflet`](/documentation/leaflet)
  217. - [`ui.line_plot`](/documentation/line_plot)
  218. - [`ui.linear_progress`](/documentation/linear_progress)
  219. - [`ui.link`](/documentation/link), `ui.link_target`
  220. - [`ui.list`](/documentation/list)
  221. - [`ui.log`](/documentation/log)
  222. - [`ui.markdown`](/documentation/markdown)
  223. - [`ui.matplotlib`](/documentation/matplotlib)
  224. - [`ui.menu`](/documentation/menu), `ui.menu_item`
  225. - [`ui.mermaid`](/documentation/mermaid)
  226. - [`ui.notification`](/documentation/notification)
  227. - [`ui.number`](/documentation/number)
  228. - [`ui.pagination`](/documentation/pagination)
  229. - [`ui.plotly`](/documentation/plotly)
  230. - [`ui.pyplot`](/documentation/pyplot)
  231. - [`ui.radio`](/documentation/radio)
  232. - [`ui.rating`](/documentation/rating)
  233. - [`ui.range`](/documentation/range)
  234. - [`ui.restructured_text`](/documentation/restructured_text)
  235. - [`ui.row`](/documentation/row)
  236. - [`ui.scene`](/documentation/scene), [`ui.scene_view`](/documentation/scene#scene_view)
  237. - [`ui.scroll_area`](/documentation/scroll_area)
  238. - [`ui.select`](/documentation/select)
  239. - [`ui.separator`](/documentation/separator)
  240. - [`ui.skeleton`](/documentation/skeleton)
  241. - [`ui.slide_item`](/documentation/slide_item)
  242. - [`ui.slider`](/documentation/slider)
  243. - [`ui.space`](/documentation/space)
  244. - [`ui.spinner`](/documentation/spinner)
  245. - [`ui.splitter`](/documentation/splitter)
  246. - [`ui.stepper`](/documentation/stepper), `ui.step`, `ui.stepper_navigation`
  247. - [`ui.switch`](/documentation/switch)
  248. - [`ui.tabs`](/documentation/tabs), `ui.tab`, `ui.tab_panels`, `ui.tab_panel`
  249. - [`ui.table`](/documentation/table)
  250. - [`ui.textarea`](/documentation/textarea)
  251. - [`ui.time`](/documentation/time)
  252. - [`ui.timeline`](/documentation/timeline), `ui.timeline_entry`
  253. - [`ui.toggle`](/documentation/toggle)
  254. - [`ui.tooltip`](/documentation/tooltip)
  255. - [`ui.tree`](/documentation/tree)
  256. - [`ui.upload`](/documentation/upload)
  257. - [`ui.video`](/documentation/video)
  258. - special layout [elements](/documentation/page_layout):
  259. - `ui.header`
  260. - `ui.footer`
  261. - `ui.drawer`, `ui.left_drawer`, `ui.right_drawer`
  262. - `ui.page_sticky`
  263. - special functions and objects:
  264. - [`ui.add_body_html`](/documentation/section_pages_routing#add_html_to_the_page) and
  265. [`ui.add_head_html`](/documentation/section_pages_routing#add_html_to_the_page): add HTML to the body and head of the page
  266. - [`ui.add_css`](/documentation/add_style#add_css_style_definitions_to_the_page),
  267. [`ui.add_sass`](/documentation/add_style#add_sass_style_definitions_to_the_page) and
  268. [`ui.add_scss`](/documentation/add_style#add_scss_style_definitions_to_the_page): add CSS, SASS and SCSS to the page
  269. - [`ui.clipboard`](/documentation/clipboard): interact with the browser's clipboard
  270. - [`ui.colors`](/documentation/colors): define the main color theme for a page
  271. - `ui.context`: get the current UI context including the `client` and `request` objects
  272. - [`ui.dark_mode`](/documentation/dark_mode): get and set the dark mode on a page
  273. - [`ui.download`](/documentation/download): download a file to the client
  274. - [`ui.fullscreen`](/documentation/fullscreen): enter, exit and toggle fullscreen mode
  275. - [`ui.keyboard`](/documentation/keyboard): define keyboard event handlers
  276. - [`ui.navigate`](/documentation/navigate): let the browser navigate to another location
  277. - [`ui.notify`](/documentation/notification): show a notification
  278. - [`ui.on`](/documentation/generic_events#custom_events): register an event handler
  279. - [`ui.page_title`](/documentation/page_title): change the current page title
  280. - [`ui.query`](/documentation/query): query HTML elements on the client side to modify props, classes and style definitions
  281. - [`ui.run`](/documentation/run) and `ui.run_with`: run the app (standalone or attached to a FastAPI app)
  282. - [`ui.run_javascript`](/documentation/run#run_custom_javascript_on_the_client_side): run custom JavaScript on the client side (can use `getElement()`, `getHtmlElement()`, and `emitEvent()`)
  283. - [`ui.teleport`](/documentation/teleport): teleport an element to a different location in the HTML DOM
  284. - [`ui.timer`](/documentation/timer): run a function periodically or once after a delay
  285. - `ui.update`: send updates of multiple elements to the client
  286. - decorators:
  287. - [`ui.page`](/documentation/page): define a page (in contrast to the automatically generated "auto-index page")
  288. - [`ui.refreshable`](/documentation/refreshable), `ui.refreshable_method`: define refreshable UI containers
  289. (can use [`ui.state`](/documentation/refreshable#refreshable_ui_with_reactive_state))
  290. #### `app`
  291. App-wide storage, mount points and lifecycle hooks.
  292. - [`app.storage`](/documentation/storage):
  293. - `app.storage.tab`: stored in memory on the server, unique per tab
  294. - `app.storage.client`: stored in memory on the server, unique per client connected to a page
  295. - `app.storage.user`: stored in a file on the server, unique per browser
  296. - `app.storage.general`: stored in a file on the server, shared across the entire app
  297. - `app.storage.browser`: stored in the browser's local storage, unique per browser
  298. - [lifecycle hooks](/documentation/section_action_events#events):
  299. - `app.on_connect()`: called when a client connects
  300. - `app.on_disconnect()`: called when a client disconnects
  301. - `app.on_startup()`: called when the app starts
  302. - `app.on_shutdown()`: called when the app shuts down
  303. - `app.on_exception()`: called when an exception occurs
  304. - [`app.shutdown()`](/documentation/section_action_events#shut_down_nicegui): shut down the app
  305. - static files:
  306. - [`app.add_static_files()`](/documentation/section_pages_routing#add_a_directory_of_static_files),
  307. `app.add_static_file()`: serve static files
  308. - [`app.add_media_files()`](/documentation/section_pages_routing#add_directory_of_media_files),
  309. `app.add_media_file()`: serve media files (supports streaming)
  310. - [`app.native`](/documentation/section_configuration_deployment#native_mode): configure the app when running in native mode
  311. #### `html`
  312. [Pure HTML elements](/documentation/html#other_html_elements):
  313. `a`,
  314. `abbr`,
  315. `acronym`,
  316. `address`,
  317. `area`,
  318. `article`,
  319. `aside`,
  320. `audio`,
  321. `b`,
  322. `basefont`,
  323. `bdi`,
  324. `bdo`,
  325. `big`,
  326. `blockquote`,
  327. `br`,
  328. `button`,
  329. `canvas`,
  330. `caption`,
  331. `cite`,
  332. `code`,
  333. `col`,
  334. `colgroup`,
  335. `data`,
  336. `datalist`,
  337. `dd`,
  338. `del_`,
  339. `details`,
  340. `dfn`,
  341. `dialog`,
  342. `div`,
  343. `dl`,
  344. `dt`,
  345. `em`,
  346. `embed`,
  347. `fieldset`,
  348. `figcaption`,
  349. `figure`,
  350. `footer`,
  351. `form`,
  352. `h1`,
  353. `header`,
  354. `hgroup`,
  355. `hr`,
  356. `i`,
  357. `iframe`,
  358. `img`,
  359. `input_`,
  360. `ins`,
  361. `kbd`,
  362. `label`,
  363. `legend`,
  364. `li`,
  365. `main`,
  366. `map_`,
  367. `mark`,
  368. `menu`,
  369. `meter`,
  370. `nav`,
  371. `object_`,
  372. `ol`,
  373. `optgroup`,
  374. `option`,
  375. `output`,
  376. `p`,
  377. `param`,
  378. `picture`,
  379. `pre`,
  380. `progress`,
  381. `q`,
  382. `rp`,
  383. `rt`,
  384. `ruby`,
  385. `s`,
  386. `samp`,
  387. `search`,
  388. `section`,
  389. `select`,
  390. `small`,
  391. `source`,
  392. `span`,
  393. `strong`,
  394. `sub`,
  395. `summary`,
  396. `sup`,
  397. `svg`,
  398. `table`,
  399. `tbody`,
  400. `td`,
  401. `template`,
  402. `textarea`,
  403. `tfoot`,
  404. `th`,
  405. `thead`,
  406. `time`,
  407. `tr`,
  408. `track`,
  409. `u`,
  410. `ul`,
  411. `var`,
  412. `video`,
  413. `wbr`
  414. #### `background_tasks`
  415. Run async functions in the background.
  416. - `create()`: create a background task
  417. - `create_lazy()`: prevent two tasks with the same name from running at the same time
  418. - `await_on_shutdown`: mark a coroutine function to be awaited during shutdown (by default all background tasks are cancelled)
  419. #### `run`
  420. Run IO and CPU bound functions in separate threads and processes.
  421. - [`run.cpu_bound()`](/documentation/section_action_events#running_cpu-bound_tasks): run a CPU-bound function in a separate process
  422. - [`run.io_bound()`](/documentation/section_action_events#running_i_o-bound_tasks): run an IO-bound function in a separate thread
  423. #### `binding`
  424. [Bind properties of objects to each other](/documentation/section_binding_properties).
  425. - [`binding.BindableProperty`](/documentation/section_binding_properties#bindable_properties_for_maximum_performance): bindable properties for maximum performance
  426. - [`binding.bindable_dataclass()`](/documentation/section_binding_properties#bindable_dataclass): create a dataclass with bindable properties
  427. - `binding.bind()`, `binding.bind_from()`, `binding.bind_to()`: methods to bind two properties
  428. #### `observables`
  429. Observable collections that notify observers when their contents change.
  430. - `ObservableCollection`: base class
  431. - `ObservableDict`: an observable dictionary
  432. - `ObservableList`: an observable list
  433. - `ObservableSet`: an observable set
  434. #### `testing`
  435. Write automated UI tests which run in a headless browser (slow) or fully simulated in Python (fast).
  436. - [`Screen`](/documentation/section_testing#screen_fixture) fixture: start a real (headless) browser to interact with your application
  437. - [`User`](/documentation/section_testing#user_fixture) fixture: simulate user interaction on a Python level (fast)
  438. ''').classes('map-of-nicegui arrow-links bold-links')