main.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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('''
  55. dark-box min-h-screen no-wrap
  56. justify-center items-center flex-col md:flex-row
  57. p-8 px-4 sm:px-8 lg:px-16
  58. gap-8 sm:gap-16 md:gap-8 lg:gap-16
  59. '''):
  60. link_target('about')
  61. with ui.column().classes('text-white max-w-4xl'):
  62. heading('Interact with Python through buttons, dialogs, 3D scenes, plots and much more.')
  63. with ui.column().classes('gap-2 bold-links arrow-links text-lg'):
  64. ui.markdown(
  65. 'NiceGUI handles all the web development details for you. '
  66. 'So you can focus on writing Python code. '
  67. 'Anything from short scripts and dashboards to full robotics projects, IoT solutions, '
  68. 'smart home automations and machine learning projects can benefit from having all code in one place.'
  69. )
  70. ui.markdown(
  71. 'Available as '
  72. '[PyPI package](https://pypi.org/project/nicegui/), '
  73. '[Docker image](https://hub.docker.com/r/zauberzeug/nicegui) and on '
  74. '[GitHub](https://github.com/zauberzeug/nicegui).')
  75. demo_card.create()
  76. with ui.column().classes('w-full q-pa-xl q-mb-xl bold-links'):
  77. link_target('features', '-50px')
  78. section_heading('Features', 'Code *nicely*')
  79. with ui.row().classes('w-full no-wrap text-lg leading-tight justify-between q-mb-xl'):
  80. features('swap_horiz', 'Interaction', [
  81. 'buttons, switches, sliders, inputs, ...',
  82. 'notifications, dialogs and menus',
  83. 'keyboard input',
  84. 'on-screen joystick',
  85. ])
  86. features('space_dashboard', 'Layout', [
  87. 'navigation bars, tabs, panels, ...',
  88. 'grouping with rows, columns and cards',
  89. 'HTML and markdown elements',
  90. 'flex layout by default',
  91. ])
  92. features('insights', 'Visualization', [
  93. 'charts, diagrams and tables',
  94. '3D scenes',
  95. 'progress bars',
  96. 'built-in timer for data refresh',
  97. ])
  98. with ui.row().classes('w-full no-wrap text-lg leading-tight justify-between'):
  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 q-pa-xl q-mb-xl'):
  118. link_target('installation', '-50px')
  119. section_heading('Installation', 'Get *started*')
  120. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  121. with ui.column().classes('w-1/3 gap-2'):
  122. ui.html('<em>1.</em>').classes('text-3xl text-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-1/3 gap-2'):
  131. ui.html('<em>2.</em>').classes('text-3xl text-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-1/3 gap-2'):
  136. ui.html('<em>3.</em>').classes('text-3xl text-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 q-pa-xl q-mb-xl'):
  141. link_target('examples', '-50px')
  142. section_heading('Examples', 'Try *this*')
  143. with ui.row().classes('justify-center w-full'), ui.column().classes('w-[65rem]'):
  144. reference.create_intro()
  145. with ui.row().classes('dark-box items-center gap-28 px-32 py-16'):
  146. with ui.column().classes('gap-2'):
  147. ui.markdown('Browse through plenty of live examples.').classes('text-3xl text-white font-medium')
  148. ui.html('Fun-Fact: This whole website is also coded with NiceGUI.').classes('text-xl text-white')
  149. ui.link('API reference', '/reference').classes('rounded-full mx-auto px-12 py-2 text-xl text-bold bg-white')
  150. with ui.column().classes('w-full q-pa-xl q-mb-xl'):
  151. link_target('demos', '-50px')
  152. section_heading('In-depth demonstrations', 'Pick your *solution*')
  153. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  154. with ui.column().classes('w-1/3'):
  155. example_link('Slideshow', 'implements a keyboard-controlled image slideshow')
  156. example_link('Authentication', 'shows how to use sessions to build a login screen')
  157. example_link(
  158. 'Modularization',
  159. 'provides an example of how to modularize your application into multiple files and reuse code')
  160. example_link(
  161. 'FastAPI',
  162. 'illustrates the integration of NiceGUI with an existing FastAPI application')
  163. with ui.column().classes('w-1/3'):
  164. example_link(
  165. 'Map',
  166. 'demonstrates wrapping the JavaScript library [leaflet](https://leafletjs.com/) to display a map at specific locations')
  167. example_link(
  168. 'AI Interface',
  169. 'utilizes the [replicate](https://replicate.com) library to perform voice-to-text transcription and generate images from prompts with Stable Diffusion')
  170. example_link('3D Scene', 'creates a webGL view and loads an STL mesh illuminated with a spotlight')
  171. with ui.column().classes('w-1/3'):
  172. example_link('Custom Vue Component', 'shows how to write and integrate a custom Vue component')
  173. example_link('Image Mask Overlay', 'shows how to overlay an image with a mask')
  174. example_link('Infinite Scroll', 'presents an infinitely scrolling image gallery')
  175. with ui.row().classes('dark-box h-screen items-center gap-28 p-32 no-wrap'):
  176. link_target('why')
  177. with ui.column().classes('gap-8'):
  178. heading('Why?')
  179. with ui.column().classes('gap-2 text-xl text-white bold-links arrow-links'):
  180. ui.markdown(
  181. 'We like '
  182. '[Streamlit](https://streamlit.io/) '
  183. 'but find it does '
  184. '[too much magic](https://github.com/zauberzeug/nicegui/issues/1#issuecomment-847413651) '
  185. 'when it comes to state handling. '
  186. 'In search for an alternative nice library to write simple graphical user interfaces in Python we discovered '
  187. '[JustPy](https://justpy.io/). '
  188. 'Although we liked the approach, it is too "low-level HTML" for our daily usage. '
  189. 'But it inspired us to use '
  190. '[Vue](https://vuejs.org/) '
  191. 'and '
  192. '[Quasar](https://quasar.dev/) '
  193. 'for the frontend.')
  194. ui.markdown(
  195. 'We have built on top of '
  196. '[FastAPI](https://fastapi.tiangolo.com/), '
  197. 'which itself is based on the ASGI framework '
  198. '[Starlette](https://www.starlette.io/) '
  199. 'and the ASGI webserver '
  200. '[Uvicorn](https://www.uvicorn.org/) '
  201. 'because of their great performance and ease of use.'
  202. )
  203. svg.face().classes('stroke-white w-[1500px]')
  204. @ui.page('/reference')
  205. def reference_page():
  206. add_head_html()
  207. add_header()
  208. reference.create_full()
  209. ui.run(uvicorn_reload_includes='*.py, *.css, *.html')