main.py 15 KB

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