1
0

main.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. import os
  3. from pathlib import Path
  4. from fastapi import Request
  5. from starlette.middleware.sessions import SessionMiddleware
  6. import prometheus
  7. from nicegui import app, ui
  8. from website import anti_scroll_hack, documentation, fly, main_page, svg
  9. prometheus.start_monitor(app)
  10. # session middleware is required for demo in documentation and prometheus
  11. app.add_middleware(SessionMiddleware, secret_key=os.environ.get('NICEGUI_SECRET_KEY', ''))
  12. on_fly = fly.setup()
  13. anti_scroll_hack.setup()
  14. app.add_static_files('/favicon', str(Path(__file__).parent / 'website' / 'favicon'))
  15. app.add_static_files('/fonts', str(Path(__file__).parent / 'website' / 'fonts'))
  16. app.add_static_files('/static', str(Path(__file__).parent / 'website' / 'static'))
  17. app.add_static_file(local_file=svg.PATH / 'logo.png', url_path='/logo.png')
  18. app.add_static_file(local_file=svg.PATH / 'logo_square.png', url_path='/logo_square.png')
  19. documentation.build_search_index()
  20. @app.post('/dark_mode')
  21. async def _post_dark_mode(request: Request) -> None:
  22. app.storage.browser['dark_mode'] = (await request.json()).get('value')
  23. @ui.page('/')
  24. def _main_page() -> None:
  25. main_page.create()
  26. @ui.page('/documentation')
  27. def _documentation_page() -> None:
  28. documentation.render_page(documentation.registry[''], with_menu=False)
  29. @ui.page('/documentation/{name}')
  30. def _documentation_detail_page(name: str) -> None:
  31. documentation.render_page(documentation.registry[name])
  32. @app.get('/status')
  33. def _status():
  34. return 'Ok'
  35. # NOTE: do not reload on fly.io (see https://github.com/zauberzeug/nicegui/discussions/1720#discussioncomment-7288741)
  36. ui.run(uvicorn_reload_includes='*.py, *.css, *.html', reload=not on_fly, reconnect_timeout=10.0)