main.py 14 KB

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