1
0

redir.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Utilities to handle redirection to browser UI."""
  2. import time
  3. import webbrowser
  4. import httpx
  5. from reflex.utils import net
  6. from .. import constants
  7. from . import console
  8. def open_browser(target_url: str) -> None:
  9. """Open a browser window to target_url.
  10. Args:
  11. target_url: The URL to open in the browser.
  12. """
  13. if not webbrowser.open(target_url):
  14. console.warn(
  15. f"Unable to automatically open the browser. Please navigate to {target_url} in your browser."
  16. )
  17. def open_browser_and_wait(
  18. target_url: str, poll_url: str, interval: int = 2
  19. ) -> httpx.Response:
  20. """Open a browser window to target_url and request poll_url until it returns successfully.
  21. Args:
  22. target_url: The URL to open in the browser.
  23. poll_url: The URL to poll for success.
  24. interval: The interval in seconds to wait between polling.
  25. Returns:
  26. The response from the poll_url.
  27. """
  28. open_browser(target_url)
  29. console.info("[b]Complete the workflow in the browser to continue.[/b]")
  30. while True:
  31. try:
  32. response = net.get(poll_url, follow_redirects=True)
  33. if response.is_success:
  34. break
  35. except httpx.RequestError as err:
  36. console.info(f"Will retry after error occurred while polling: {err}.")
  37. time.sleep(interval)
  38. return response
  39. def reflex_build_redirect() -> None:
  40. """Open the browser window to reflex.build."""
  41. open_browser(constants.Templates.REFLEX_BUILD_FRONTEND)