__init__.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from ..errors import MissingRequirementsError
  2. try:
  3. from .server.website import Website
  4. from .server.backend_api import Backend_Api
  5. from .server.app import create_app
  6. import_error = None
  7. except ImportError as e:
  8. import_error = e
  9. def get_gui_app(demo: bool = False):
  10. if import_error is not None:
  11. raise MissingRequirementsError(f'Install "gui" requirements | pip install -U g4f[gui]\n{import_error}')
  12. app = create_app()
  13. app.demo = demo
  14. site = Website(app)
  15. for route in site.routes:
  16. app.add_url_rule(
  17. route,
  18. view_func=site.routes[route]['function'],
  19. methods=site.routes[route]['methods'],
  20. )
  21. backend_api = Backend_Api(app)
  22. for route in backend_api.routes:
  23. app.add_url_rule(
  24. route,
  25. view_func = backend_api.routes[route]['function'],
  26. methods = backend_api.routes[route]['methods'],
  27. )
  28. return app
  29. def run_gui(host: str = '0.0.0.0', port: int = 8080, debug: bool = False) -> None:
  30. config = {
  31. 'host' : host,
  32. 'port' : port,
  33. 'debug': debug
  34. }
  35. app = get_gui_app()
  36. print(f"Running on port {config['port']}")
  37. app.run(**config)
  38. print(f"Closing port {config['port']}")