-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
browser: New module for reasonably ergonomic web browser interaction
Based on `nextstrain view`'s interaction with the standard library's webbrowser module. Arranges to launch the browser in a separate thread, as this will be good enough for many contexts. `nextstrain view` itself still arranges to launch the browser in a separate _process_, since view's main thread/process exec's into a new program shortly after launching the browser.
- Loading branch information
Showing
2 changed files
with
53 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Web browser interaction. | ||
""" | ||
import webbrowser | ||
from threading import Thread, ThreadError | ||
from os import environ | ||
from .util import warn | ||
|
||
|
||
# Avoid text-mode browsers | ||
TERM = environ.pop("TERM", None) | ||
try: | ||
BROWSER = webbrowser.get() | ||
except: | ||
BROWSER = None | ||
finally: | ||
if TERM is not None: | ||
environ["TERM"] = TERM | ||
|
||
|
||
def open_browser(url: str, new_thread: bool = True): | ||
""" | ||
Opens *url* in a web browser. | ||
Opens in a new tab, if possible, and raises the window to the top, if | ||
possible. | ||
Launches the browser from a separate thread by default so waiting on the | ||
browser child process doesn't block the main (or calling) thread. Set | ||
*new_thread* to False to launch from the same thread as the caller (e.g. if | ||
you've already spawned a dedicated thread or process for the browser). | ||
Note that some registered browsers launch in the background themselves, but | ||
not all do, so this feature makes launch behaviour consistent across | ||
browsers. | ||
Prints a warning to stderr if a browser can't be found or can't be | ||
launched, as automatically opening a browser is considered a | ||
nice-but-not-necessary feature. | ||
""" | ||
if not BROWSER: | ||
warn(f"Couldn't open <{url}> in browser: no browser found") | ||
return | ||
|
||
try: | ||
if new_thread: | ||
Thread(target = open_browser, args = (url, False), daemon = True).start() | ||
else: | ||
# new = 2 means new tab, if possible | ||
BROWSER.open(url, new = 2, autoraise = True) | ||
except (ThreadError, webbrowser.Error) as err: | ||
warn(f"Couldn't open <{url}> in browser: {err!r}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters