main.py 13 KB

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