|
1 | 1 | """Utilities to handle redirection to browser UI.""" |
2 | 2 |
|
3 | | -import time |
4 | | -import webbrowser |
| 3 | +from typing import TYPE_CHECKING |
5 | 4 |
|
6 | | -from reflex import constants |
7 | | -from reflex.utils import net |
| 5 | +if TYPE_CHECKING: |
| 6 | + from urllib.parse import SplitResult |
8 | 7 |
|
9 | | -from . import console |
10 | 8 |
|
11 | | - |
12 | | -def open_browser(target_url: str) -> None: |
| 9 | +def open_browser(target_url: "SplitResult") -> None: |
13 | 10 | """Open a browser window to target_url. |
14 | 11 |
|
15 | 12 | Args: |
16 | 13 | target_url: The URL to open in the browser. |
17 | 14 | """ |
18 | | - if not webbrowser.open(target_url): |
| 15 | + import webbrowser |
| 16 | + |
| 17 | + from reflex.utils import console |
| 18 | + |
| 19 | + if not webbrowser.open(target_url.geturl()): |
19 | 20 | console.warn( |
20 | 21 | f"Unable to automatically open the browser. Please navigate to {target_url} in your browser." |
21 | 22 | ) |
22 | 23 | else: |
23 | | - console.info(f"Opening browser to {target_url}.") |
24 | | - |
25 | | - |
26 | | -def open_browser_and_wait(target_url: str, poll_url: str, interval: int = 2): |
27 | | - """Open a browser window to target_url and request poll_url until it returns successfully. |
28 | | -
|
29 | | - Args: |
30 | | - target_url: The URL to open in the browser. |
31 | | - poll_url: The URL to poll for success. |
32 | | - interval: The interval in seconds to wait between polling. |
33 | | -
|
34 | | - Returns: |
35 | | - The response from the poll_url. |
36 | | - """ |
37 | | - import httpx |
38 | | - |
39 | | - open_browser(target_url) |
40 | | - console.info("[b]Complete the workflow in the browser to continue.[/b]") |
41 | | - while True: |
42 | | - try: |
43 | | - response = net.get(poll_url, follow_redirects=True) |
44 | | - if response.is_success: |
45 | | - break |
46 | | - except httpx.RequestError as err: |
47 | | - console.info(f"Will retry after error occurred while polling: {err}.") |
48 | | - time.sleep(interval) |
49 | | - return response |
| 24 | + simplified_url = target_url._replace(path="", query="", fragment="").geturl() |
| 25 | + console.info(f"Opened browser to {simplified_url}") |
50 | 26 |
|
51 | 27 |
|
52 | 28 | def reflex_build_redirect() -> None: |
53 | 29 | """Open the browser window to reflex.build.""" |
54 | | - open_browser(constants.Templates.REFLEX_BUILD_FRONTEND) |
| 30 | + from urllib.parse import urlsplit |
| 31 | + |
| 32 | + from reflex import constants |
| 33 | + |
| 34 | + open_browser(urlsplit(constants.Templates.REFLEX_BUILD_FRONTEND_WITH_REFERRER)) |
55 | 35 |
|
56 | 36 |
|
57 | 37 | def reflex_templates(): |
58 | 38 | """Open the browser window to reflex.build/templates.""" |
59 | | - open_browser(constants.Templates.REFLEX_TEMPLATES_URL) |
| 39 | + from urllib.parse import urlsplit |
| 40 | + |
| 41 | + from reflex import constants |
| 42 | + |
| 43 | + open_browser(urlsplit(constants.Templates.REFLEX_TEMPLATES_URL)) |
0 commit comments