main.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. ui.add_static_files('/favicon', Path(__file__).parent / 'website' / 'favicon')
  9. def add_head_html() -> None:
  10. ui.add_head_html('<meta name="viewport" content="width=device-width, initial-scale=1" />')
  11. ui.add_head_html(docutils.core.publish_parts('', writer_name='html')['stylesheet'])
  12. ui.add_head_html(f'<style>{HtmlFormatter(nobackground=True).get_style_defs(".codehilite")}</style>')
  13. ui.add_head_html('''
  14. <link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png">
  15. <link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png">
  16. <link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
  17. <link rel="manifest" href="/favicon/site.webmanifest">
  18. <link rel="mask-icon" href="/favicon/safari-pinned-tab.svg" color="#000000">
  19. <link rel="shortcut icon" href="/favicon/favicon.ico">
  20. <meta name="msapplication-TileColor" content="#ffffff">
  21. <meta name="msapplication-config" content="/favicon/browserconfig.xml">
  22. <meta name="theme-color" content="#ffffff">
  23. ''') # https://realfavicongenerator.net/
  24. ui.add_head_html(f'''
  25. <style>
  26. body {{
  27. background-color: #f8f8f8;
  28. }}
  29. em {{
  30. font-style: normal;
  31. color: {ACCENT_COLOR};
  32. }}
  33. </style>
  34. ''')
  35. def add_header() -> None:
  36. with ui.header() \
  37. .classes('items-center') \
  38. .style(f'background-color: {ACCENT_COLOR}; height: {HEADER_HEIGHT}; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1)'):
  39. ui.html((STATIC / 'happy_face.svg').read_text()).classes('w-8 stroke-white')
  40. with ui.link(target=index_page):
  41. ui.html((STATIC / 'nicegui_word.svg').read_text()).classes('w-24')
  42. with ui.row().classes('items-center ml-auto'):
  43. ui.link('Features').classes('text-lg').style('color: white!important')
  44. ui.link('Installation').classes('text-lg').style('color: white!important')
  45. ui.link('Documentation').classes('text-lg').style('color: white!important')
  46. ui.link('API Reference', reference_page).classes('text-lg').style('color: white!important')
  47. ui.link('Docker').classes('text-lg').style('color: white!important')
  48. ui.link('Deployment').classes('text-lg').style('color: white!important')
  49. with ui.link(target='https://github.com/zauberzeug/nicegui/'):
  50. ui.html((STATIC / 'github.svg').read_text()).classes('fill-white scale-125 m-1')
  51. @ui.page('/')
  52. async def index_page(client: Client):
  53. client.content.classes(remove='q-pa-md gap-4')
  54. add_head_html()
  55. add_header()
  56. with ui.row() \
  57. .classes('w-full q-pa-md items-center gap-12 no-wrap') \
  58. .style(f'height: calc(100vh - {HEADER_HEIGHT}); transform: translateX(-250px)'):
  59. ui.html((STATIC / 'happy_face.svg').read_text()).classes('stroke-black').style('width: 500px')
  60. with ui.column().classes('gap-8'):
  61. ui.html('Meet the <em>NiceGUI</em>.') \
  62. .style('font-size: 400%; line-height: 0.9; font-weight: 500')
  63. ui.markdown('An easy-to-use Python-based UI framework\n\nwhich shows up in your web browser.') \
  64. .style('font-size: 200%; line-height: 0.9')
  65. with ui.row() \
  66. .classes('w-full q-pa-md items-center gap-28 p-32 no-wrap') \
  67. .style(f'height: calc(100vh - {HEADER_HEIGHT}); background: {ACCENT_COLOR}'):
  68. with ui.column().classes('gap-6'):
  69. ui.markdown('Create buttons, dialogs, markdown,\n\n3D scenes, plots and much more at ease.') \
  70. .style('font-size: 300%; color: white; line-height: 0.9; font-weight: 500').classes('mb-4')
  71. ui.label('''
  72. It is great for micro web apps, dashboards, robotics projects, smart home solutions
  73. and similar use cases. You can also use it in development, for example when
  74. tweaking/configuring a machine learning algorithm or tuning motor controllers.
  75. ''').style('font-size: 150%; color: white').classes('leading-tight')
  76. with ui.row().style('font-size: 150%; color: white').classes('leading-tight gap-2'):
  77. ui.html('''
  78. NiceGUI is available as
  79. <a href="https://pypi.org/project/nicegui/"><strong>PyPI package</strong><span class="material-icons">north_east</span></a>,
  80. <a href="https://hub.docker.com/r/zauberzeug/nicegui"><strong>Docker image</strong><span class="material-icons">north_east</span></a> and on
  81. <a href="https://github.com/zauberzeug/nicegui"><strong>GitHub</strong><span class="material-icons">north_east</span></a>.
  82. ''')
  83. demo_card.create()
  84. with ui.row().classes('w-full q-pa-md'):
  85. reference.create_intro()
  86. with ui.row() \
  87. .classes('w-full items-center gap-28 px-32 py-16 no-wrap') \
  88. .style(f'background: {ACCENT_COLOR}'):
  89. with ui.column().classes('gap-4'):
  90. ui.markdown('Go to the API reference to see a ton of live examples') \
  91. .style('font-size: 220%; color: white; line-height: 0.9; font-weight: 500')
  92. ui.label('The whole content of https://nicegui.io/ is implemented with NiceGUI itself.') \
  93. .style('font-size: 150%; color: white')
  94. ui.link('API reference', '/reference') \
  95. .classes('rounded-full mx-auto px-12 py-2 text-xl text-bold bg-white')
  96. with ui.column().classes('w-full q-pa-xl'):
  97. ui.label('In-depth demonstration').classes('text-bold text-lg')
  98. ui.html('What else can you do with <em>NiceGUI</em>?') \
  99. .style('font-size: 300%; font-weight: 500; margin-top: -20px')
  100. with ui.row().classes('w-full no-wrap text-lg leading-tight'):
  101. with ui.column().classes('w-1/3'):
  102. ui.markdown(
  103. 'You may also have a look at the following examples for in-depth demonstrations of what you can do with NiceGUI:')
  104. example_link('Slideshow', 'implements a keyboard-controlled image slideshow')
  105. example_link('Authentication', 'shows how to use sessions to build a login screen')
  106. example_link(
  107. 'Modularization',
  108. 'provides an example of how to modularize your application into multiple files and create a specialized page decorator')
  109. with ui.column().classes('w-1/3'):
  110. example_link('Map', 'uses the JavaScript library leaflet to display a map at specific locations')
  111. example_link(
  112. 'AI Interface',
  113. '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')
  114. example_link('3D Scene', 'creates a 3D scene and loads an STL mesh illuminated with a spotlight')
  115. with ui.column().classes('w-1/3'):
  116. example_link('Custom Vue Component', 'shows how to write and integrate a custom vue component')
  117. example_link('Image Mask Overlay', 'shows how to overlay an image with a mask')
  118. example_link('Infinite Scroll', 'shows an infinitely scrolling image gallery')
  119. def example_link(title: str, description: str) -> None:
  120. name = title.lower().replace(' ', '_')
  121. with ui.column().classes('gap-0'):
  122. ui.link(title, f'https://github.com/zauberzeug/nicegui/tree/main/examples/{name}/main.py') \
  123. .classes(replace='text-black text-bold')
  124. ui.markdown(description)
  125. @ui.page('/reference')
  126. def reference_page():
  127. add_head_html()
  128. add_header()
  129. reference.create_full()
  130. ui.run()