overview.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.keyboard`](/documentation/keyboard): define keyboard event handlers
  273. - [`ui.navigate`](/documentation/navigate): let the browser navigate to another location
  274. - [`ui.notify`](/documentation/notification): show a notification
  275. - [`ui.on`](/documentation/generic_events#custom_events): register an event handler
  276. - [`ui.page_title`](/documentation/page_title): change the current page title
  277. - [`ui.query`](/documentation/query): query HTML elements on the client side to modify props, classes and style definitions
  278. - [`ui.run`](/documentation/run) and `ui.run_with`: run the app (standalone or attached to a FastAPI app)
  279. - [`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()`)
  280. - [`ui.teleport`](/documentation/teleport): teleport an element to a different location in the HTML DOM
  281. - [`ui.timer`](/documentation/timer): run a function periodically or once after a delay
  282. - `ui.update`: send updates of multiple elements to the client
  283. - decorators:
  284. - [`ui.page`](/documentation/page): define a page (in contrast to the automatically generated "auto-index page")
  285. - [`ui.refreshable`](/documentation/refreshable), `ui.refreshable_method`: define refreshable UI containers
  286. (can use [`ui.state`](/documentation/refreshable#refreshable_ui_with_reactive_state))
  287. #### `app`
  288. App-wide storage, mount points and lifecycle hooks.
  289. - [`app.storage`](/documentation/storage):
  290. - `app.storage.tab`: stored in memory on the server, unique per tab
  291. - `app.storage.client`: stored in memory on the server, unique per client connected to a page
  292. - `app.storage.user`: stored in a file on the server, unique per browser
  293. - `app.storage.general`: stored in a file on the server, shared across the entire app
  294. - `app.storage.browser`: stored in the browser's local storage, unique per browser
  295. - [lifecycle hooks](/documentation/section_action_events#events):
  296. - `app.on_connect()`: called when a client connects
  297. - `app.on_disconnect()`: called when a client disconnects
  298. - `app.on_startup()`: called when the app starts
  299. - `app.on_shutdown()`: called when the app shuts down
  300. - `app.on_exception()`: called when an exception occurs
  301. - [`app.shutdown()`](/documentation/section_action_events#shut_down_nicegui): shut down the app
  302. - static files:
  303. - [`app.add_static_files()`](/documentation/section_pages_routing#add_a_directory_of_static_files),
  304. `app.add_static_file()`: serve static files
  305. - [`app.add_media_files()`](/documentation/section_pages_routing#add_directory_of_media_files),
  306. `app.add_media_file()`: serve media files (supports streaming)
  307. - [`app.native`](/documentation/section_configuration_deployment#native_mode): configure the app when running in native mode
  308. #### `html`
  309. [Pure HTML elements](/documentation/html#other_html_elements):
  310. `a`,
  311. `abbr`,
  312. `acronym`,
  313. `address`,
  314. `area`,
  315. `article`,
  316. `aside`,
  317. `audio`,
  318. `b`,
  319. `basefont`,
  320. `bdi`,
  321. `bdo`,
  322. `big`,
  323. `blockquote`,
  324. `br`,
  325. `button`,
  326. `canvas`,
  327. `caption`,
  328. `cite`,
  329. `code`,
  330. `col`,
  331. `colgroup`,
  332. `data`,
  333. `datalist`,
  334. `dd`,
  335. `del_`,
  336. `details`,
  337. `dfn`,
  338. `dialog`,
  339. `div`,
  340. `dl`,
  341. `dt`,
  342. `em`,
  343. `embed`,
  344. `fieldset`,
  345. `figcaption`,
  346. `figure`,
  347. `footer`,
  348. `form`,
  349. `h1`,
  350. `header`,
  351. `hgroup`,
  352. `hr`,
  353. `i`,
  354. `iframe`,
  355. `img`,
  356. `input_`,
  357. `ins`,
  358. `kbd`,
  359. `label`,
  360. `legend`,
  361. `li`,
  362. `main`,
  363. `map_`,
  364. `mark`,
  365. `menu`,
  366. `meter`,
  367. `nav`,
  368. `object_`,
  369. `ol`,
  370. `optgroup`,
  371. `option`,
  372. `output`,
  373. `p`,
  374. `param`,
  375. `picture`,
  376. `pre`,
  377. `progress`,
  378. `q`,
  379. `rp`,
  380. `rt`,
  381. `ruby`,
  382. `s`,
  383. `samp`,
  384. `search`,
  385. `section`,
  386. `select`,
  387. `small`,
  388. `source`,
  389. `span`,
  390. `strong`,
  391. `sub`,
  392. `summary`,
  393. `sup`,
  394. `svg`,
  395. `table`,
  396. `tbody`,
  397. `td`,
  398. `template`,
  399. `textarea`,
  400. `tfoot`,
  401. `th`,
  402. `thead`,
  403. `time`,
  404. `tr`,
  405. `track`,
  406. `u`,
  407. `ul`,
  408. `var`,
  409. `video`,
  410. `wbr`
  411. #### `background_tasks`
  412. Run async functions in the background.
  413. - `create()`: create a background task
  414. - `create_lazy()`: prevent two tasks with the same name from running at the same time
  415. #### `run`
  416. Run IO and CPU bound functions in separate threads and processes.
  417. - [`run.cpu_bound()`](/documentation/section_action_events#running_cpu-bound_tasks): run a CPU-bound function in a separate process
  418. - [`run.io_bound()`](/documentation/section_action_events#running_i_o-bound_tasks): run an IO-bound function in a separate thread
  419. #### `observables`
  420. Observable collections that notify observers when their contents change.
  421. - `ObservableCollection`: base class
  422. - `ObservableDict`: an observable dictionary
  423. - `ObservableList`: an observable list
  424. - `ObservableSet`: an observable set
  425. #### `testing`
  426. Write automated UI tests which run in a headless browser (slow) or fully simulated in Python (fast).
  427. - [`Screen`](/documentation/section_testing#screen_fixture) fixture: start a real (headless) browser to interact with your application
  428. - [`User`](/documentation/section_testing#user_fixture) fixture: simulate user interaction on a Python level (fast)
  429. ''').classes('map-of-nicegui arrow-links bold-links')