main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/usr/bin/env python3
  2. if True:
  3. # increasing max decode packets to be able to transfer images
  4. # see https://github.com/miguelgrinberg/python-engineio/issues/142
  5. from engineio.payload import Payload
  6. Payload.max_decode_packets = 500
  7. from pathlib import Path
  8. from pygments.formatters import HtmlFormatter
  9. from nicegui import Client, ui
  10. from website import demo_card, reference, svg
  11. from website.example import bash_window, browser_window, python_window
  12. from website.style import example_link, features, heading, link_target, section_heading, subtitle, title
  13. ui.add_static_files('/favicon', Path(__file__).parent / 'website' / 'favicon')
  14. ui.add_static_files('/fonts', Path(__file__).parent / 'website' / 'fonts')
  15. def add_head_html() -> None:
  16. ui.add_head_html((Path(__file__).parent / 'website' / 'static' / 'header.html').read_text())
  17. ui.add_head_html(f'<style>{HtmlFormatter(nobackground=True).get_style_defs(".codehilite")}</style>')
  18. ui.add_head_html(f"<style>{(Path(__file__).parent / 'website' / 'static' / 'style.css').read_text()}</style>")
  19. def add_header() -> None:
  20. menu_items = {
  21. 'Features': '/#features',
  22. 'Installation': '/#installation',
  23. 'Examples': '/#examples',
  24. 'API Reference': '/reference',
  25. 'Demos': '/#demos',
  26. 'Why?': '/#why',
  27. }
  28. with ui.header() \
  29. .classes('items-center duration-200 p-0 px-4 no-wrap') \
  30. .style('box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1)'):
  31. with ui.link(target=index_page).classes('row gap-4 items-center no-wrap mr-auto'):
  32. svg.face().classes('w-8 stroke-white stroke-2')
  33. svg.word().classes('w-24')
  34. with ui.row().classes('lg:hidden'):
  35. with ui.menu().classes('bg-primary text-white text-lg') as menu:
  36. for title, target in menu_items.items():
  37. ui.menu_item(title, on_click=lambda _, target=target: ui.open(target))
  38. ui.button(on_click=menu.open).props('flat color=white icon=menu')
  39. with ui.row().classes('max-lg:hidden'):
  40. for title, target in menu_items.items():
  41. ui.link(title, target).classes(replace='text-lg text-white')
  42. with ui.link(target='https://github.com/zauberzeug/nicegui/'):
  43. svg.github().classes('fill-white scale-125 m-1')
  44. @ui.page('/')
  45. async def index_page(client: Client):
  46. client.content.classes(remove='q-pa-md gap-4')
  47. add_head_html()
  48. add_header()
  49. with ui.row().classes('w-full h-screen items-center gap-8 pr-4 no-wrap into-section'):
  50. svg.face(half=True).classes('stroke-black w-[200px] md:w-[230px] lg:w-[300px]')
  51. with ui.column().classes('gap-4 md:gap-8 pt-32'):
  52. title('Meet the *NiceGUI*.')
  53. subtitle('And let any browser be the frontend of your Python code.') \
  54. .classes('max-w-[20rem] sm:max-w-[24rem] md:max-w-[30rem]')
  55. ui.link(target='#about').classes('scroll-indicator')
  56. with ui.row().classes('''
  57. dark-box min-h-screen no-wrap
  58. justify-center items-center flex-col md:flex-row
  59. p-8 lg:p-16
  60. gap-8 sm:gap-16 md:gap-8 lg:gap-16
  61. '''):
  62. link_target('about')
  63. with ui.column().classes('text-white max-w-4xl'):
  64. heading('Interact with Python through buttons, dialogs, 3D&nbsp;scenes, plots and much more.')
  65. with ui.column().classes('gap-2 bold-links arrow-links text-lg'):
  66. ui.markdown(
  67. 'NiceGUI handles all the web development details for you. '
  68. 'So you can focus on writing Python code. '
  69. 'Anything from short scripts and dashboards to full robotics projects, IoT solutions, '
  70. 'smart home automations and machine learning projects can benefit from having all code in one place.'
  71. )
  72. ui.markdown(
  73. 'Available as '
  74. '[PyPI package](https://pypi.org/project/nicegui/), '
  75. '[Docker image](https://hub.docker.com/r/zauberzeug/nicegui) and on '
  76. '[GitHub](https://github.com/zauberzeug/nicegui).')
  77. demo_card.create()
  78. with ui.column().classes('w-full p-8 lg:p-16 bold-links arrow-links max-w-[1600px] mx-auto'):
  79. link_target('features', '-50px')
  80. section_heading('Features', 'Code *nicely*')
  81. with ui.row().classes('w-full text-lg leading-tight grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 gap-8'):
  82. features('swap_horiz', 'Interaction', [
  83. 'buttons, switches, sliders, inputs, ...',
  84. 'notifications, dialogs and menus',
  85. 'keyboard input',
  86. 'on-screen joystick',
  87. ])
  88. features('space_dashboard', 'Layout', [
  89. 'navigation bars, tabs, panels, ...',
  90. 'grouping with rows, columns and cards',
  91. 'HTML and markdown elements',
  92. 'flex layout by default',
  93. ])
  94. features('insights', 'Visualization', [
  95. 'charts, diagrams and tables',
  96. '3D scenes',
  97. 'progress bars',
  98. 'built-in timer for data refresh',
  99. ])
  100. features('brush', 'Styling', [
  101. 'customizable color themes',
  102. 'custom CSS and classes',
  103. 'modern look with material design',
  104. 'built-in [Tailwind](https://tailwindcss.com/) support',
  105. ])
  106. features('source', 'Coding', [
  107. 'live-cycle events',
  108. 'implicit reload on code change',
  109. 'straight-forward data binding',
  110. 'execute javascript from Python',
  111. ])
  112. features('anchor', 'Foundation', [
  113. 'generic [Vue](https://vuejs.org/) to Python bridge',
  114. 'dynamic GUI through [Quasar](https://quasar.dev/)',
  115. 'content is served with [FastAPI](http://fastapi.tiangolo.com/)',
  116. 'Python 3.7+',
  117. ])
  118. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1600px] mx-auto'):
  119. link_target('installation', '-50px')
  120. section_heading('Installation', 'Get *started*')
  121. with ui.row().classes('w-full text-lg leading-tight grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-8'):
  122. with ui.column().classes('w-full max-w-md gap-2'):
  123. ui.html('<em>1.</em>').classes('text-3xl font-bold')
  124. ui.markdown('Create __main.py__').classes('text-lg')
  125. with python_window(classes='w-full h-52'):
  126. ui.markdown('''```python\n
  127. from nicegui import ui
  128. ui.label('Hello NiceGUI!')
  129. ui.run()
  130. ```''')
  131. with ui.column().classes('w-full max-w-md gap-2'):
  132. ui.html('<em>2.</em>').classes('text-3xl font-bold')
  133. ui.markdown('Install and launch').classes('text-lg')
  134. with bash_window(classes='w-full h-52'):
  135. ui.markdown('```bash\npip3 install nicegui\npython3 main.py\n```')
  136. with ui.column().classes('w-full max-w-md gap-2'):
  137. ui.html('<em>3.</em>').classes('text-3xl font-bold')
  138. ui.markdown('Enjoy!').classes('text-lg')
  139. with browser_window(classes='w-full h-52'):
  140. ui.label('Hello NiceGUI!')
  141. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1600px] mx-auto'):
  142. link_target('examples', '-50px')
  143. section_heading('Examples', 'Try *this*')
  144. with ui.column().classes('w-full'):
  145. reference.create_intro()
  146. with ui.column().classes('dark-box p-8 lg:p-16 my-16'):
  147. with ui.column().classes('mx-auto items-center gap-y-8 gap-x-32 lg:flex-row'):
  148. with ui.column().classes('gap-1 max-lg:items-center max-lg:text-center'):
  149. ui.markdown('Browse through plenty of live examples.') \
  150. .classes('text-white text-2xl md:text-3xl font-medium')
  151. ui.html('Fun-Fact: This whole website is also coded with NiceGUI.') \
  152. .classes('text-white text-lg md:text-xl')
  153. ui.link('API reference', '/reference') \
  154. .classes('rounded-full mx-auto px-12 py-2 text-white bg-white font-medium text-lg md:text-xl')
  155. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1600px] mx-auto'):
  156. link_target('demos', '-50px')
  157. section_heading('In-depth demonstrations', 'Pick your *solution*')
  158. with ui.row().classes('w-full text-lg leading-tight grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 gap-4'):
  159. example_link('Slideshow', 'implements a keyboard-controlled image slideshow')
  160. example_link('Authentication', 'shows how to use sessions to build a login screen')
  161. example_link(
  162. 'Modularization',
  163. 'provides an example of how to modularize your application into multiple files and reuse code')
  164. example_link(
  165. 'FastAPI',
  166. 'illustrates the integration of NiceGUI with an existing FastAPI application')
  167. example_link(
  168. 'Map',
  169. 'demonstrates wrapping the JavaScript library [leaflet](https://leafletjs.com/) to display a map at specific locations')
  170. example_link(
  171. 'AI Interface',
  172. 'utilizes the [replicate](https://replicate.com) library to perform voice-to-text transcription and generate images from prompts with Stable Diffusion')
  173. example_link('3D Scene', 'creates a webGL view and loads an STL mesh illuminated with a spotlight')
  174. example_link('Custom Vue Component', 'shows how to write and integrate a custom Vue component')
  175. example_link('Image Mask Overlay', 'shows how to overlay an image with a mask')
  176. example_link('Infinite Scroll', 'presents an infinitely scrolling image gallery')
  177. with ui.row().classes('bg-primary w-full min-h-screen mt-16'):
  178. link_target('why')
  179. with ui.column().classes('''
  180. max-w-[1600px] p-8 lg:p-16 m-auto
  181. items-center justify-center no-wrap flex-col md:flex-row gap-16
  182. '''):
  183. with ui.column().classes('gap-8'):
  184. heading('Why?')
  185. with ui.column().classes('gap-2 text-xl text-white bold-links arrow-links'):
  186. ui.markdown(
  187. 'We at '
  188. '[Zauberzeug](https://zauberzeug.com) '
  189. 'like '
  190. '[Streamlit](https://streamlit.io/) '
  191. 'but find it does '
  192. '[too much magic](https://github.com/zauberzeug/nicegui/issues/1#issuecomment-847413651) '
  193. 'when it comes to state handling. '
  194. 'In search for an alternative nice library to write simple graphical user interfaces in Python we discovered '
  195. '[JustPy](https://justpy.io/). '
  196. 'Although we liked the approach, it is too "low-level HTML" for our daily usage. '
  197. 'But it inspired us to use '
  198. '[Vue](https://vuejs.org/) '
  199. 'and '
  200. '[Quasar](https://quasar.dev/) '
  201. 'for the frontend.')
  202. ui.markdown(
  203. 'We have built on top of '
  204. '[FastAPI](https://fastapi.tiangolo.com/), '
  205. 'which itself is based on the ASGI framework '
  206. '[Starlette](https://www.starlette.io/) '
  207. 'and the ASGI webserver '
  208. '[Uvicorn](https://www.uvicorn.org/) '
  209. 'because of their great performance and ease of use.'
  210. )
  211. svg.face().classes('stroke-white shrink-0 w-[200px] md:w-[300px] lg:w-[450px]')
  212. @ui.page('/reference')
  213. def reference_page():
  214. add_head_html()
  215. add_header()
  216. ui.add_head_html('<style>html {scroll-behavior: auto;}</style>')
  217. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1250px] mx-auto'):
  218. section_heading('Documentation and Examples', '*API* Reference')
  219. ui.markdown(
  220. 'This is the API reference for NiceGUI >= 1.0. '
  221. 'Documentation for older versions can be found at [https://0.9.nicegui.io/](https://0.9.nicegui.io/reference).'
  222. ).classes('bold-links arrow-links')
  223. reference.create_full()
  224. ui.run(uvicorn_reload_includes='*.py, *.css, *.html')