main.py 12 KB

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