main.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. body {{
  28. background-color: #f8f8f8;
  29. }}
  30. em {{
  31. font-style: normal;
  32. color: {ACCENT_COLOR};
  33. }}
  34. </style>
  35. ''')
  36. def add_header() -> None:
  37. with ui.header() \
  38. .classes('items-center') \
  39. .style(f'background-color: {ACCENT_COLOR}; height: {HEADER_HEIGHT}; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1)'):
  40. ui.html((STATIC / 'happy_face.svg').read_text()).classes('w-8 stroke-white')
  41. with ui.link(target=index_page):
  42. ui.html((STATIC / 'nicegui_word.svg').read_text()).classes('w-24')
  43. with ui.row().classes('items-center ml-auto'):
  44. ui.link('Features').classes('text-lg').style('color: white!important')
  45. ui.link('Installation').classes('text-lg').style('color: white!important')
  46. ui.link('Documentation').classes('text-lg').style('color: white!important')
  47. ui.link('API Reference', reference_page).classes('text-lg').style('color: white!important')
  48. ui.link('Docker').classes('text-lg').style('color: white!important')
  49. ui.link('Deployment').classes('text-lg').style('color: white!important')
  50. with ui.link(target='https://github.com/zauberzeug/nicegui/'):
  51. ui.html((STATIC / 'github.svg').read_text()).classes('fill-white scale-125 m-1')
  52. @ui.page('/')
  53. async def index_page(client: Client):
  54. client.content.classes(remove='q-pa-md gap-4')
  55. add_head_html()
  56. add_header()
  57. with ui.row() \
  58. .classes('w-full q-pa-md items-center gap-12 no-wrap') \
  59. .style(f'height: calc(100vh - {HEADER_HEIGHT}); transform: translateX(-250px)'):
  60. ui.html((STATIC / 'happy_face.svg').read_text()).classes('stroke-black').style('width: 500px')
  61. with ui.column().classes('gap-8'):
  62. ui.html('Meet the <em>NiceGUI</em>.') \
  63. .style('font-size: 400%; line-height: 0.9; font-weight: 500')
  64. ui.markdown('An easy-to-use Python-based UI framework\n\nwhich shows up in your web browser.') \
  65. .style('font-size: 200%; line-height: 0.9')
  66. with ui.row() \
  67. .classes('w-full q-pa-md items-center gap-28 p-32 no-wrap') \
  68. .style(f'height: calc(100vh - {HEADER_HEIGHT}); background: {ACCENT_COLOR}'):
  69. with ui.column().classes('gap-6'):
  70. ui.markdown('Create buttons, dialogs, markdown,\n\n3D scenes, plots and much more at ease.') \
  71. .style('font-size: 300%; color: white; line-height: 0.9; font-weight: 500').classes('mb-4')
  72. ui.label('''
  73. It is great for micro web apps, dashboards, robotics projects, smart home solutions
  74. and similar use cases. You can also use it in development, for example when
  75. tweaking/configuring a machine learning algorithm or tuning motor controllers.
  76. ''').style('font-size: 150%; color: white').classes('leading-tight')
  77. with ui.row().style('font-size: 150%; color: white').classes('leading-tight gap-2'):
  78. ui.html('''
  79. NiceGUI is available as
  80. <a href="https://pypi.org/project/nicegui/"><strong>PyPI package</strong><span class="material-icons">north_east</span></a>,
  81. <a href="https://hub.docker.com/r/zauberzeug/nicegui"><strong>Docker image</strong><span class="material-icons">north_east</span></a> and on
  82. <a href="https://github.com/zauberzeug/nicegui"><strong>GitHub</strong><span class="material-icons">north_east</span></a>.
  83. ''')
  84. demo_card.create()
  85. with ui.column().classes('w-full q-pa-xl'):
  86. ui.label('Installation').classes('text-bold text-lg')
  87. ui.html('Getting <em>started</em>') \
  88. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  89. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  90. with ui.column().classes('w-1/3 gap-2'):
  91. ui.html('<em>1.</em>').classes('text-3xl text-bold')
  92. ui.markdown('Install').classes('text-lg')
  93. with bash_window().classes('w-full h-52'):
  94. ui.markdown('```bash\npython3 -m pip install nicegui\n```')
  95. with ui.column().classes('w-1/3 gap-2'):
  96. ui.html('<em>2.</em>').classes('text-3xl text-bold')
  97. ui.markdown('Write file __main.py__').classes('text-lg')
  98. with python_window().classes('w-full h-52'):
  99. ui.markdown('''```python\n
  100. from nicegui import ui
  101. ui.label('Hello NiceGUI!')
  102. ui.run()
  103. ```''')
  104. with ui.column().classes('w-1/3 gap-2'):
  105. ui.html('<em>3.</em>').classes('text-3xl text-bold')
  106. ui.markdown('Launch it with').classes('text-lg')
  107. with bash_window().classes('w-full h-52'):
  108. ui.markdown('```bash\npython3 main.py\n```')
  109. with ui.column().classes('w-full q-pa-xl'):
  110. ui.label('Documentation').classes('text-bold text-lg')
  111. ui.html('Interactive <em>Examples</em>') \
  112. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  113. reference.create_intro()
  114. with ui.row() \
  115. .classes('w-full items-center gap-28 px-32 py-16 no-wrap') \
  116. .style(f'background: {ACCENT_COLOR}'):
  117. with ui.column().classes('gap-4'):
  118. ui.markdown('Go to the API reference to see a ton of live examples') \
  119. .style('font-size: 220%; color: white; line-height: 0.9; font-weight: 500')
  120. ui.html('The whole content of <a href="https://nicegui.io/">nicegui.io</a> is implemented with NiceGUI itself.') \
  121. .style('font-size: 150%; color: white')
  122. ui.link('API reference', '/reference') \
  123. .classes('rounded-full mx-auto px-12 py-2 text-xl text-bold bg-white')
  124. with ui.column().classes('w-full q-pa-xl'):
  125. ui.label('In-depth demonstration').classes('text-bold text-lg')
  126. ui.html('What else can you do with <em>NiceGUI</em>?') \
  127. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  128. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  129. with ui.column().classes('w-1/3'):
  130. ui.markdown(
  131. 'You may also have a look at the following examples for in-depth demonstrations of what you can do with NiceGUI:')
  132. example_link('Slideshow', 'implements a keyboard-controlled image slideshow')
  133. example_link('Authentication', 'shows how to use sessions to build a login screen')
  134. example_link(
  135. 'Modularization',
  136. 'provides an example of how to modularize your application into multiple files and create a specialized page decorator')
  137. with ui.column().classes('w-1/3'):
  138. example_link('Map', 'uses the JavaScript library leaflet to display a map at specific locations')
  139. example_link(
  140. 'AI Interface',
  141. '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')
  142. example_link('3D Scene', 'creates a 3D scene and loads an STL mesh illuminated with a spotlight')
  143. with ui.column().classes('w-1/3'):
  144. example_link('Custom Vue Component', 'shows how to write and integrate a custom vue component')
  145. example_link('Image Mask Overlay', 'shows how to overlay an image with a mask')
  146. example_link('Infinite Scroll', 'shows an infinitely scrolling image gallery')
  147. with ui.row() \
  148. .classes('w-full q-pa-md items-center gap-28 p-32 no-wrap') \
  149. .style(f'height: calc(100vh - {HEADER_HEIGHT}); background: {ACCENT_COLOR}'):
  150. with ui.column().classes('gap-6'):
  151. ui.markdown('Why?') \
  152. .style('font-size: 300%; color: white; line-height: 0.9; font-weight: 500').classes('mb-4')
  153. ui.html('''
  154. We like
  155. <strong><a href="https://streamlit.io/">Streamlit</a></strong>
  156. but find it does
  157. <strong><a href="https://github.com/zauberzeug/nicegui/issues/1#issuecomment-847413651">too much magic</a></strong>
  158. when it comes to state handling.
  159. In search for an alternative nice library to write simple graphical user interfaces in Python we discovered
  160. <strong><a href="https://justpy.io/">JustPy</a></strong>.
  161. Although we liked the approach, it is too "low-level HTML" for our daily usage.
  162. Therefore we created NiceGUI on top of
  163. <strong><a href="https://fastapi.tiangolo.com/">FastAPI</a></strong>,
  164. which itself is based on the ASGI framework
  165. <strong><a href="https://www.starlette.io/">Starlette</a></strong>,
  166. and the ASGI webserver
  167. <strong><a href="https://www.uvicorn.org/">Uvicorn</a></strong>.
  168. ''').style('font-size: 150%; color: white').classes('leading-tight')
  169. ui.html((STATIC / 'happy_face.svg').read_text()).classes('stroke-white').style('width: 1500px')
  170. def example_link(title: str, description: str) -> None:
  171. name = title.lower().replace(' ', '_')
  172. with ui.column().classes('gap-0'):
  173. ui.link(title, f'https://github.com/zauberzeug/nicegui/tree/main/examples/{name}/main.py') \
  174. .classes(replace='text-black text-bold')
  175. ui.markdown(description)
  176. @ui.page('/reference')
  177. def reference_page():
  178. add_head_html()
  179. add_header()
  180. reference.create_full()
  181. ui.run()