main.py 11 KB

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