-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Possibility to set preferred browser and theme (#202)
- Loading branch information
1 parent
fb3356c
commit 8765458
Showing
11 changed files
with
134 additions
and
27 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
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
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,15 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
|
||
def user_data_dir() -> Path: | ||
"""Returns platform specific path to store user application data | ||
""" | ||
|
||
if sys.platform == "win32": | ||
return Path.home() / "Application Data" / "webviz" | ||
|
||
if sys.platform == "darwin": | ||
return Path.home() / "Library" / "Application Support" / "webviz" | ||
|
||
return Path.home() / ".local" / "share" / "webviz" |
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,52 @@ | ||
import os | ||
import json | ||
import webbrowser | ||
from typing import Optional | ||
|
||
from ._user_data_dir import user_data_dir | ||
from .themes import installed_themes | ||
|
||
USER_SETTINGS_FILE = user_data_dir() / "user_settings.json" | ||
|
||
|
||
def set_user_preferences( | ||
theme: Optional[str] = None, browser: Optional[str] = None | ||
) -> None: | ||
|
||
preferences = ( | ||
json.loads(USER_SETTINGS_FILE.read_text()) | ||
if USER_SETTINGS_FILE.is_file() | ||
else {} | ||
) | ||
|
||
new_preferences = {} | ||
|
||
if theme is not None: | ||
if theme not in installed_themes: | ||
raise ValueError( | ||
f"Theme {theme} is not one of the installed themes ({', '.join(installed_themes)})" | ||
) | ||
new_preferences["theme"] = theme | ||
|
||
if browser is not None: | ||
try: | ||
webbrowser.get(using=browser) | ||
except webbrowser.Error: | ||
raise ValueError( | ||
f"Could not find an installed browser with the name {browser}." | ||
) | ||
|
||
new_preferences["browser"] = browser | ||
|
||
if new_preferences: | ||
preferences.update(new_preferences) | ||
os.makedirs(USER_SETTINGS_FILE.parent, exist_ok=True) | ||
USER_SETTINGS_FILE.write_text(json.dumps(preferences)) | ||
|
||
|
||
def get_user_preference(setting: str) -> Optional[str]: | ||
return ( | ||
json.loads(USER_SETTINGS_FILE.read_text()).get(setting) | ||
if USER_SETTINGS_FILE.is_file() | ||
else None | ||
) |
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 @@ | ||
from ._localhost_certificate import LocalhostCertificate |
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
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
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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from ._localhost_open_browser import LocalhostOpenBrowser | ||
from ._available_port import get_available_port | ||
from ._silence_flask_startup import silence_flask_startup | ||
from ._dash_component_utils import calculate_slider_step |
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