overview.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.range`](/documentation/range)
  233. - [`ui.restructured_text`](/documentation/restructured_text)
  234. - [`ui.row`](/documentation/row)
  235. - [`ui.scene`](/documentation/scene), [`ui.scene_view`](/documentation/scene#scene_view)
  236. - [`ui.scroll_area`](/documentation/scroll_area)
  237. - [`ui.select`](/documentation/select)
  238. - [`ui.separator`](/documentation/separator)
  239. - [`ui.skeleton`](/documentation/skeleton)
  240. - [`ui.slider`](/documentation/slider)
  241. - [`ui.space`](/documentation/space)
  242. - [`ui.spinner`](/documentation/spinner)
  243. - [`ui.splitter`](/documentation/splitter)
  244. - [`ui.stepper`](/documentation/stepper), `ui.step`, `ui.stepper_navigation`
  245. - [`ui.switch`](/documentation/switch)
  246. - [`ui.tabs`](/documentation/tabs), `ui.tab`, `ui.tab_panels`, `ui.tab_panel`
  247. - [`ui.table`](/documentation/table)
  248. - [`ui.textarea`](/documentation/textarea)
  249. - [`ui.time`](/documentation/time)
  250. - [`ui.timeline`](/documentation/timeline), `ui.timeline_entry`
  251. - [`ui.toggle`](/documentation/toggle)
  252. - [`ui.tooltip`](/documentation/tooltip)
  253. - [`ui.tree`](/documentation/tree)
  254. - [`ui.upload`](/documentation/upload)
  255. - [`ui.video`](/documentation/video)
  256. - special layout [elements](/documentation/page_layout):
  257. - `ui.header`
  258. - `ui.footer`
  259. - `ui.drawer`, `ui.left_drawer`, `ui.right_drawer`
  260. - `ui.page_sticky`
  261. - special functions and objects:
  262. - [`ui.add_body_html`](/documentation/section_pages_routing#add_html_to_the_page) and
  263. [`ui.add_head_html`](/documentation/section_pages_routing#add_html_to_the_page): add HTML to the body and head of the page
  264. - [`ui.add_css`](/documentation/add_style#add_css_style_definitions_to_the_page),
  265. [`ui.add_sass`](/documentation/add_style#add_sass_style_definitions_to_the_page) and
  266. [`ui.add_scss`](/documentation/add_style#add_scss_style_definitions_to_the_page): add CSS, SASS and SCSS to the page
  267. - [`ui.clipboard`](/documentation/clipboard): interact with the browser's clipboard
  268. - [`ui.colors`](/documentation/colors): define the main color theme for a page
  269. - `ui.context`: get the current UI context including the `client` and `request` objects
  270. - [`ui.dark_mode`](/documentation/dark_mode): get and set the dark mode on a page
  271. - [`ui.download`](/documentation/download): download a file to the client
  272. - [`ui.fullscreen`](/documentation/fullscreen): enter, exit and toggle fullscreen mode
  273. - [`ui.keyboard`](/documentation/keyboard): define keyboard event handlers
  274. - [`ui.navigate`](/documentation/navigate): let the browser navigate to another location
  275. - [`ui.notify`](/documentation/notification): show a notification
  276. - [`ui.on`](/documentation/generic_events#custom_events): register an event handler
  277. - [`ui.page_title`](/documentation/page_title): change the current page title
  278. - [`ui.query`](/documentation/query): query HTML elements on the client side to modify props, classes and style definitions
  279. - [`ui.run`](/documentation/run) and `ui.run_with`: run the app (standalone or attached to a FastAPI app)
  280. - [`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()`)
  281. - [`ui.teleport`](/documentation/teleport): teleport an element to a different location in the HTML DOM
  282. - [`ui.timer`](/documentation/timer): run a function periodically or once after a delay
  283. - `ui.update`: send updates of multiple elements to the client
  284. - decorators:
  285. - [`ui.page`](/documentation/page): define a page (in contrast to the automatically generated "auto-index page")
  286. - [`ui.refreshable`](/documentation/refreshable), `ui.refreshable_method`: define refreshable UI containers
  287. (can use [`ui.state`](/documentation/refreshable#refreshable_ui_with_reactive_state))
  288. #### `app`
  289. App-wide storage, mount points and lifecycle hooks.
  290. - [`app.storage`](/documentation/storage):
  291. - `app.storage.tab`: stored in memory on the server, unique per tab
  292. - `app.storage.client`: stored in memory on the server, unique per client connected to a page
  293. - `app.storage.user`: stored in a file on the server, unique per browser
  294. - `app.storage.general`: stored in a file on the server, shared across the entire app
  295. - `app.storage.browser`: stored in the browser's local storage, unique per browser
  296. - [lifecycle hooks](/documentation/section_action_events#events):
  297. - `app.on_connect()`: called when a client connects
  298. - `app.on_disconnect()`: called when a client disconnects
  299. - `app.on_startup()`: called when the app starts
  300. - `app.on_shutdown()`: called when the app shuts down
  301. - `app.on_exception()`: called when an exception occurs
  302. - [`app.shutdown()`](/documentation/section_action_events#shut_down_nicegui): shut down the app
  303. - static files:
  304. - [`app.add_static_files()`](/documentation/section_pages_routing#add_a_directory_of_static_files),
  305. `app.add_static_file()`: serve static files
  306. - [`app.add_media_files()`](/documentation/section_pages_routing#add_directory_of_media_files),
  307. `app.add_media_file()`: serve media files (supports streaming)
  308. - [`app.native`](/documentation/section_configuration_deployment#native_mode): configure the app when running in native mode
  309. #### `html`
  310. [Pure HTML elements](/documentation/html#other_html_elements):
  311. `a`,
  312. `abbr`,
  313. `acronym`,
  314. `address`,
  315. `area`,
  316. `article`,
  317. `aside`,
  318. `audio`,
  319. `b`,
  320. `basefont`,
  321. `bdi`,
  322. `bdo`,
  323. `big`,
  324. `blockquote`,
  325. `br`,
  326. `button`,
  327. `canvas`,
  328. `caption`,
  329. `cite`,
  330. `code`,
  331. `col`,
  332. `colgroup`,
  333. `data`,
  334. `datalist`,
  335. `dd`,
  336. `del_`,
  337. `details`,
  338. `dfn`,
  339. `dialog`,
  340. `div`,
  341. `dl`,
  342. `dt`,
  343. `em`,
  344. `embed`,
  345. `fieldset`,
  346. `figcaption`,
  347. `figure`,
  348. `footer`,
  349. `form`,
  350. `h1`,
  351. `header`,
  352. `hgroup`,
  353. `hr`,
  354. `i`,
  355. `iframe`,
  356. `img`,
  357. `input_`,
  358. `ins`,
  359. `kbd`,
  360. `label`,
  361. `legend`,
  362. `li`,
  363. `main`,
  364. `map_`,
  365. `mark`,
  366. `menu`,
  367. `meter`,
  368. `nav`,
  369. `object_`,
  370. `ol`,
  371. `optgroup`,
  372. `option`,
  373. `output`,
  374. `p`,
  375. `param`,
  376. `picture`,
  377. `pre`,
  378. `progress`,
  379. `q`,
  380. `rp`,
  381. `rt`,
  382. `ruby`,
  383. `s`,
  384. `samp`,
  385. `search`,
  386. `section`,
  387. `select`,
  388. `small`,
  389. `source`,
  390. `span`,
  391. `strong`,
  392. `sub`,
  393. `summary`,
  394. `sup`,
  395. `svg`,
  396. `table`,
  397. `tbody`,
  398. `td`,
  399. `template`,
  400. `textarea`,
  401. `tfoot`,
  402. `th`,
  403. `thead`,
  404. `time`,
  405. `tr`,
  406. `track`,
  407. `u`,
  408. `ul`,
  409. `var`,
  410. `video`,
  411. `wbr`
  412. #### `background_tasks`
  413. Run async functions in the background.
  414. - `create()`: create a background task
  415. - `create_lazy()`: prevent two tasks with the same name from running at the same time
  416. #### `run`
  417. Run IO and CPU bound functions in separate threads and processes.
  418. - [`run.cpu_bound()`](/documentation/section_action_events#running_cpu-bound_tasks): run a CPU-bound function in a separate process
  419. - [`run.io_bound()`](/documentation/section_action_events#running_i_o-bound_tasks): run an IO-bound function in a separate thread
  420. #### `binding`
  421. [Bind properties of objects to each other](/documentation/section_binding_properties).
  422. - [`binding.BindableProperty`](/documentation/section_binding_properties#bindable_properties_for_maximum_performance): bindable properties for maximum performance
  423. - [`binding.bindable_dataclass()`](/documentation/section_binding_properties#bindable_dataclass): create a dataclass with bindable properties
  424. - `binding.bind()`, `binding.bind_from()`, `binding.bind_to()`: methods to bind two properties
  425. #### `observables`
  426. Observable collections that notify observers when their contents change.
  427. - `ObservableCollection`: base class
  428. - `ObservableDict`: an observable dictionary
  429. - `ObservableList`: an observable list
  430. - `ObservableSet`: an observable set
  431. #### `testing`
  432. Write automated UI tests which run in a headless browser (slow) or fully simulated in Python (fast).
  433. - [`Screen`](/documentation/section_testing#screen_fixture) fixture: start a real (headless) browser to interact with your application
  434. - [`User`](/documentation/section_testing#user_fixture) fixture: simulate user interaction on a Python level (fast)
  435. ''').classes('map-of-nicegui arrow-links bold-links')