main.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. # pylint: disable=missing-function-docstring
  3. import os
  4. from pathlib import Path
  5. from fastapi import Request
  6. from starlette.middleware.sessions import SessionMiddleware
  7. import prometheus
  8. from nicegui import app, ui
  9. from website import anti_scroll_hack, documentation_pages, fly, main_page, svg
  10. prometheus.start_monitor(app)
  11. # session middleware is required for demo in documentation and prometheus
  12. app.add_middleware(SessionMiddleware, secret_key=os.environ.get('NICEGUI_SECRET_KEY', ''))
  13. on_fly = fly.setup()
  14. anti_scroll_hack.setup()
  15. app.add_static_files('/favicon', str(Path(__file__).parent / 'website' / 'favicon'))
  16. app.add_static_files('/fonts', str(Path(__file__).parent / 'website' / 'fonts'))
  17. app.add_static_files('/static', str(Path(__file__).parent / 'website' / 'static'))
  18. app.add_static_file(local_file=svg.PATH / 'logo.png', url_path='/logo.png')
  19. app.add_static_file(local_file=svg.PATH / 'logo_square.png', url_path='/logo_square.png')
  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('/')(main_page.create)
  24. ui.page('/documentation')(documentation_pages.create_overview)
  25. ui.page('/documentation/{name}')(documentation_pages.create_page)
  26. @app.get('/status')
  27. def status():
  28. return 'Ok'
  29. # NOTE: do not reload on fly.io (see https://github.com/zauberzeug/nicegui/discussions/1720#discussioncomment-7288741)
  30. ui.run(uvicorn_reload_includes='*.py, *.css, *.html', reload=not on_fly, reconnect_timeout=10.0)