main.py 12 KB

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