__init__.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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():
  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. site = Website(app)
  14. for route in site.routes:
  15. app.add_url_rule(
  16. route,
  17. view_func=site.routes[route]['function'],
  18. methods=site.routes[route]['methods'],
  19. )
  20. backend_api = Backend_Api(app)
  21. for route in backend_api.routes:
  22. app.add_url_rule(
  23. route,
  24. view_func = backend_api.routes[route]['function'],
  25. methods = backend_api.routes[route]['methods'],
  26. )
  27. return app
  28. def run_gui(host: str = '0.0.0.0', port: int = 8080, debug: bool = False) -> None:
  29. config = {
  30. 'host' : host,
  31. 'port' : port,
  32. 'debug': debug
  33. }
  34. app = get_gui_app()
  35. print(f"Running on port {config['port']}")
  36. app.run(**config)
  37. print(f"Closing port {config['port']}")