__init__.py 1.1 KB

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