main.py 11 KB

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