main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env python3
  2. from pathlib import Path
  3. import docutils.core
  4. from pygments.formatters import HtmlFormatter
  5. from nicegui import Client, ui
  6. from website import demo_card, reference
  7. from website.constants import ACCENT_COLOR, HEADER_HEIGHT, STATIC
  8. from website.example import bash_window, python_window
  9. ui.add_static_files('/favicon', Path(__file__).parent / 'website' / 'favicon')
  10. def add_head_html() -> None:
  11. ui.add_head_html('<meta name="viewport" content="width=device-width, initial-scale=1" />')
  12. ui.add_head_html(docutils.core.publish_parts('', writer_name='html')['stylesheet'])
  13. ui.add_head_html(f'<style>{HtmlFormatter(nobackground=True).get_style_defs(".codehilite")}</style>')
  14. ui.add_head_html('''
  15. <link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png">
  16. <link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png">
  17. <link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
  18. <link rel="manifest" href="/favicon/site.webmanifest">
  19. <link rel="mask-icon" href="/favicon/safari-pinned-tab.svg" color="#000000">
  20. <link rel="shortcut icon" href="/favicon/favicon.ico">
  21. <meta name="msapplication-TileColor" content="#ffffff">
  22. <meta name="msapplication-config" content="/favicon/browserconfig.xml">
  23. <meta name="theme-color" content="#ffffff">
  24. ''') # https://realfavicongenerator.net/
  25. ui.add_head_html(f'''
  26. <style>
  27. html {{
  28. scroll-behavior: smooth;
  29. }}
  30. body {{
  31. background-color: #f8f8f8;
  32. }}
  33. em {{
  34. font-style: normal;
  35. color: {ACCENT_COLOR};
  36. }}
  37. </style>
  38. ''')
  39. ui.add_head_html(f'''
  40. <style>
  41. .q-header {{
  42. height: {HEADER_HEIGHT};
  43. background-color: {ACCENT_COLOR};
  44. }}
  45. .q-header.fade {{
  46. height: 50px;
  47. background-color: {ACCENT_COLOR}d0;
  48. backdrop-filter: blur(5px);
  49. }}
  50. </style>
  51. <script>
  52. window.onscroll = () => {{
  53. const header = document.querySelector(".q-header");
  54. if (document.documentElement.scrollTop > 50)
  55. header.classList.add("fade");
  56. else
  57. header.classList.remove("fade");
  58. }};
  59. </script>
  60. ''')
  61. def add_header() -> None:
  62. with ui.header() \
  63. .classes('items-center duration-200 px-4', remove='q-pa-md') \
  64. .style(f'box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1)'):
  65. ui.html((STATIC / 'happy_face.svg').read_text()).classes('w-8 stroke-white')
  66. with ui.link(target=index_page):
  67. ui.html((STATIC / 'nicegui_word.svg').read_text()).classes('w-24')
  68. with ui.row().classes('items-center ml-auto'):
  69. ui.link('Features', '/#features').classes(replace='text-lg text-white')
  70. ui.link('Installation', '/#installation').classes(replace='text-lg text-white')
  71. ui.link('Examples', '/#examples').classes(replace='text-lg text-white')
  72. ui.link('API Reference', reference_page).classes(replace='text-lg text-white')
  73. with ui.link(target='https://github.com/zauberzeug/nicegui/'):
  74. ui.html((STATIC / 'github.svg').read_text()).classes('fill-white scale-125 m-1')
  75. @ui.page('/')
  76. async def index_page(client: Client):
  77. client.content.classes(remove='q-pa-md gap-4')
  78. add_head_html()
  79. add_header()
  80. with ui.row() \
  81. .classes('w-full q-pa-md items-center gap-12 no-wrap') \
  82. .style(f'height: calc(100vh - {HEADER_HEIGHT}); transform: translateX(-250px)'):
  83. ui.html((STATIC / 'happy_face.svg').read_text()).classes('stroke-black').style('width: 500px')
  84. with ui.column().classes('gap-8'):
  85. ui.html('Meet the <em>NiceGUI</em>.') \
  86. .style('font-size: 400%; line-height: 0.9; font-weight: 500')
  87. ui.markdown('Your easy-to-use Python framework to create\n\nuser interfaces which show up in the browser.') \
  88. .style('font-size: 200%; line-height: 0.9')
  89. with ui.row() \
  90. .classes('w-full q-pa-md items-center gap-28 p-32 no-wrap') \
  91. .style(f'height: calc(100vh - {HEADER_HEIGHT}); background: {ACCENT_COLOR}'):
  92. with ui.column().classes('gap-6'):
  93. ui.markdown('Create buttons, dialogs, markdown,\n\n3D scenes, plots and much more at ease.') \
  94. .style('font-size: 300%; color: white; line-height: 0.9; font-weight: 500').classes('mb-4')
  95. ui.label('''
  96. It is great for micro web apps, dashboards, robotics projects, smart home solutions
  97. and similar use cases. You can also use it in development, for example when
  98. tweaking/configuring a machine learning algorithm or tuning motor controllers.
  99. ''').style('font-size: 150%; color: white').classes('leading-tight')
  100. with ui.row().style('font-size: 150%; color: white').classes('leading-tight gap-2'):
  101. ui.html('''
  102. NiceGUI is available as
  103. <a href="https://pypi.org/project/nicegui/"><strong>PyPI package</strong><span class="material-icons">north_east</span></a>,
  104. <a href="https://hub.docker.com/r/zauberzeug/nicegui"><strong>Docker image</strong><span class="material-icons">north_east</span></a> and on
  105. <a href="https://github.com/zauberzeug/nicegui"><strong>GitHub</strong><span class="material-icons">north_east</span></a>.
  106. ''')
  107. demo_card.create()
  108. ui.link_target('features').style(f'position: relative; top: -{HEADER_HEIGHT}')
  109. with ui.column().classes('w-full q-pa-xl'):
  110. ui.label('Features').classes('text-bold text-lg')
  111. ui.html('What has <em>NiceGUI</em> to offer?') \
  112. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  113. with ui.row().classes('w-full no-wrap text-lg leading-tight justify-between'):
  114. with ui.column().classes('gap-1'):
  115. ui.label('User Interface').classes('text-bold mb-4')
  116. ui.markdown('- common elements like label, button, checkbox, switch, slider, input, ...')
  117. ui.markdown('- page layout with navigation bars, tabs, panels, ...')
  118. ui.markdown('- grouping with rows, columns, cards and dialogs')
  119. ui.markdown('- HTML and markdown elements')
  120. ui.markdown('- high-level elements like charts, tables, trees, 3D scenes, joystick, ...')
  121. ui.markdown('- built-in timer to refresh data in intervals')
  122. ui.markdown('- notifications, dialogs and menus')
  123. ui.markdown('- keyboard input')
  124. with ui.column().classes('gap-1'):
  125. ui.label('Under the hood').classes('text-bold mb-4')
  126. ui.markdown('- browser-based graphical user interface')
  127. ui.markdown('- based on FastAPI and Uvicorn')
  128. ui.markdown('- live-cycle events and session data')
  129. ui.markdown('- customizable page layout and colors')
  130. with ui.column().classes('gap-1'):
  131. ui.label('Development').classes('text-bold mb-4')
  132. ui.markdown('- implicit reload on code change')
  133. ui.markdown('- straight-forward data binding')
  134. ui.link_target('installation').style(f'position: relative; top: -{HEADER_HEIGHT}')
  135. with ui.column().classes('w-full q-pa-xl'):
  136. ui.label('Installation').classes('text-bold text-lg')
  137. ui.html('Getting <em>started</em>') \
  138. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  139. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  140. with ui.column().classes('w-1/3 gap-2'):
  141. ui.html('<em>1.</em>').classes('text-3xl text-bold')
  142. ui.markdown('Install').classes('text-lg')
  143. with bash_window().classes('w-full h-52'):
  144. ui.markdown('```bash\npython3 -m pip install nicegui\n```')
  145. with ui.column().classes('w-1/3 gap-2'):
  146. ui.html('<em>2.</em>').classes('text-3xl text-bold')
  147. ui.markdown('Write file __main.py__').classes('text-lg')
  148. with python_window().classes('w-full h-52'):
  149. ui.markdown('''```python\n
  150. from nicegui import ui
  151. ui.label('Hello NiceGUI!')
  152. ui.run()
  153. ```''')
  154. with ui.column().classes('w-1/3 gap-2'):
  155. ui.html('<em>3.</em>').classes('text-3xl text-bold')
  156. ui.markdown('Launch it with').classes('text-lg')
  157. with bash_window().classes('w-full h-52'):
  158. ui.markdown('```bash\npython3 main.py\n```')
  159. ui.link_target('examples').style(f'position: relative; top: -{HEADER_HEIGHT}')
  160. with ui.column().classes('w-full q-pa-xl'):
  161. ui.label('Documentation').classes('text-bold text-lg')
  162. ui.html('Interactive <em>Examples</em>') \
  163. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  164. reference.create_intro()
  165. with ui.row() \
  166. .classes('w-full items-center gap-28 px-32 py-16 no-wrap') \
  167. .style(f'background: {ACCENT_COLOR}'):
  168. with ui.column().classes('gap-4'):
  169. ui.markdown('Go to the API reference to see a ton of live examples.') \
  170. .style('font-size: 220%; color: white; line-height: 0.9; font-weight: 500')
  171. ui.html('The whole content of <a href="https://nicegui.io/">nicegui.io</a> is implemented with NiceGUI itself.') \
  172. .style('font-size: 150%; color: white')
  173. ui.link('API reference', '/reference') \
  174. .classes('rounded-full mx-auto px-12 py-2 text-xl text-bold bg-white')
  175. with ui.column().classes('w-full q-pa-xl'):
  176. ui.label('In-depth demonstration').classes('text-bold text-lg')
  177. ui.html('What else can you do with <em>NiceGUI</em>?') \
  178. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  179. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  180. with ui.column().classes('w-1/3'):
  181. ui.markdown(
  182. 'You may also have a look at the following examples for in-depth demonstrations of what you can do with NiceGUI:')
  183. example_link('Slideshow', 'implements a keyboard-controlled image slideshow')
  184. example_link('Authentication', 'shows how to use sessions to build a login screen')
  185. example_link(
  186. 'Modularization',
  187. 'provides an example of how to modularize your application into multiple files and create a specialized page decorator')
  188. with ui.column().classes('w-1/3'):
  189. example_link('Map', 'uses the JavaScript library leaflet to display a map at specific locations')
  190. example_link(
  191. 'AI Interface',
  192. 'utilizes the great but non-async API from <https://replicate.com> to perform voice-to-text transcription and generate images from prompts with Stable Diffusion')
  193. example_link('3D Scene', 'creates a 3D scene and loads an STL mesh illuminated with a spotlight')
  194. with ui.column().classes('w-1/3'):
  195. example_link('Custom Vue Component', 'shows how to write and integrate a custom vue component')
  196. example_link('Image Mask Overlay', 'shows how to overlay an image with a mask')
  197. example_link('Infinite Scroll', 'shows an infinitely scrolling image gallery')
  198. with ui.row() \
  199. .classes('w-full q-pa-md items-center gap-28 p-32 no-wrap') \
  200. .style(f'height: calc(100vh - {HEADER_HEIGHT}); background: {ACCENT_COLOR}'):
  201. with ui.column().classes('gap-6'):
  202. ui.markdown('Why?') \
  203. .style('font-size: 300%; color: white; line-height: 0.9; font-weight: 500').classes('mb-4')
  204. ui.html('''
  205. We like
  206. <strong><a href="https://streamlit.io/">Streamlit</a></strong>
  207. but find it does
  208. <strong><a href="https://github.com/zauberzeug/nicegui/issues/1#issuecomment-847413651">too much magic</a></strong>
  209. when it comes to state handling.
  210. In search for an alternative nice library to write simple graphical user interfaces in Python we discovered
  211. <strong><a href="https://justpy.io/">JustPy</a></strong>.
  212. Although we liked the approach, it is too "low-level HTML" for our daily usage.
  213. But it inspired us to use
  214. <strong><a href="https://vuejs.org/">Vue</a></strong>
  215. and
  216. <strong><a href="https://quasar.dev/">Quasar</a></strong>
  217. for the frontend.<br/>
  218. The backend is build on top of
  219. <strong><a href="https://fastapi.tiangolo.com/">FastAPI</a></strong>,
  220. which itself is based on the ASGI framework
  221. <strong><a href="https://www.starlette.io/">Starlette</a></strong>,
  222. and the ASGI webserver
  223. <strong><a href="https://www.uvicorn.org/">Uvicorn</a></strong>.
  224. ''').style('font-size: 150%; color: white').classes('leading-tight')
  225. ui.html((STATIC / 'happy_face.svg').read_text()).classes('stroke-white').style('width: 1500px')
  226. def example_link(title: str, description: str) -> None:
  227. name = title.lower().replace(' ', '_')
  228. with ui.column().classes('gap-0'):
  229. ui.link(title, f'https://github.com/zauberzeug/nicegui/tree/main/examples/{name}/main.py') \
  230. .classes(replace='text-black text-bold')
  231. ui.markdown(description)
  232. @ui.page('/reference')
  233. def reference_page():
  234. add_head_html()
  235. add_header()
  236. reference.create_full()
  237. ui.run()