Skip to content

Commit 1e96a4f

Browse files
0dmabrichr
andauthored
feat: scrub toggle for gui (#375)
* add scrub toggle + write dark_mode to env * Update config.py * Update util.py * Update config.py * address comments * Update config.py * run isort * from first * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * add env file path (also where did the toggle go??) * Update config.py * Update config.py * Update config.py * isort * Update util.py * linted --------- Co-authored-by: Richard Abrich <[email protected]>
1 parent e1f6e5e commit 1e96a4f

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

openadapt/app/cards.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from nicegui import ui
1010

1111
from openadapt.app.objects.local_file_picker import LocalFilePicker
12-
from openadapt.app.util import set_dark, sync_switch
12+
from openadapt.app.util import get_scrub, set_dark, set_scrub, sync_switch
1313

1414
PROC = None
1515

@@ -21,13 +21,17 @@ def settings(dark_mode: bool) -> None:
2121
dark_mode (bool): Current dark mode setting.
2222
"""
2323
with ui.dialog() as settings, ui.card():
24-
s = ui.switch(
25-
"Dark mode",
26-
on_change=lambda: set_dark(dark_mode, s.value),
24+
dark_switch = ui.switch(
25+
"Dark mode", on_change=lambda: set_dark(dark_mode, dark_switch.value)
2726
)
28-
sync_switch(s, dark_mode)
29-
ui.button("Close", on_click=lambda: settings.close())
27+
sync_switch(dark_switch, dark_mode)
28+
29+
scrub_switch = ui.switch(
30+
"Scrubbing", on_change=lambda: set_scrub(scrub_switch.value)
31+
)
32+
sync_switch(scrub_switch, get_scrub())
3033

34+
ui.button("Close", on_click=lambda: settings.close())
3135
settings.open()
3236

3337

openadapt/app/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010

1111
import base64
1212
import os
13-
import threading
1413

1514
from nicegui import app, ui
1615

17-
from openadapt import replay, visualize
16+
from openadapt import config, replay, visualize
1817
from openadapt.app.cards import recording_prompt, select_import, settings
1918
from openadapt.app.objects.console import Console
2019
from openadapt.app.util import clear_db, on_export, on_import
@@ -29,6 +28,8 @@ def run_app() -> None:
2928
app.native.start_args["debug"] = False
3029

3130
dark = ui.dark_mode()
31+
dark.value = config.APP_DARK_MODE
32+
3233
logger = None
3334

3435
# Add logo
@@ -70,10 +71,9 @@ def run_app() -> None:
7071
)
7172
.tooltip("Record a new replay / Stop recording")
7273
)
73-
ui.icon("visibility", size="64px").on(
74-
"click",
75-
lambda: threading.Thread(target=visualize.main).start(),
76-
).tooltip("Visualize the latest replay")
74+
ui.icon("visibility", size="64px").on("click", visualize.main).tooltip(
75+
"Visualize the latest replay"
76+
)
7777

7878
ui.icon("play_arrow", size="64px").on(
7979
"click",

openadapt/app/util.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from nicegui import elements, ui
2121

22+
from openadapt import config
2223
from openadapt.app.objects import console
2324
from openadapt.scripts.reset_db import reset_db
2425

@@ -97,7 +98,29 @@ def sync_switch(
9798
switch: The switch object.
9899
prop: The property object.
99100
"""
100-
switch.value = prop.value
101+
switch.value = prop.value if hasattr(prop, "value") else prop
102+
103+
104+
def set_scrub(value: bool) -> None:
105+
"""Set the scrubbing value.
106+
107+
Args:
108+
value: The value to set.
109+
"""
110+
if config.SCRUB_ENABLED != value:
111+
config.persist_env("SCRUB_ENABLED", value)
112+
config.SCRUB_ENABLED = value
113+
ui.notify("Scrubbing enabled." if value else "Scrubbing disabled.")
114+
ui.notify("You may need to restart the app for this to take effect.")
115+
116+
117+
def get_scrub() -> bool:
118+
"""Get the scrubbing value.
119+
120+
Returns:
121+
bool: The scrubbing value.
122+
"""
123+
return config.SCRUB_ENABLED
101124

102125

103126
def set_dark(dark_mode: ui.dark_mode, value: bool) -> None:
@@ -107,4 +130,6 @@ def set_dark(dark_mode: ui.dark_mode, value: bool) -> None:
107130
dark_mode: The dark mode object.
108131
value: The value to set.
109132
"""
110-
dark_mode.value = value
133+
if dark_mode.value != value:
134+
dark_mode.value = value
135+
config.persist_env("APP_DARK_MODE", value)

openadapt/config.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"ACTION_TEXT_SEP": "-",
3737
"ACTION_TEXT_NAME_PREFIX": "<",
3838
"ACTION_TEXT_NAME_SUFFIX": ">",
39+
# APP CONFIGURATIONS
40+
"APP_DARK_MODE": False,
3941
# SCRUBBING CONFIGURATIONS
4042
"SCRUB_ENABLED": False,
4143
"SCRUB_CHAR": "*",
@@ -102,6 +104,8 @@
102104
list(stop_str) for stop_str in STOP_STRS
103105
] + SPECIAL_CHAR_STOP_SEQUENCES
104106

107+
ENV_FILE_PATH = ".env"
108+
105109

106110
def getenv_fallback(var_name: str) -> str:
107111
"""Get the value of an environment variable or fallback to the default value.
@@ -128,6 +132,34 @@ def getenv_fallback(var_name: str) -> str:
128132
return rval
129133

130134

135+
def persist_env(var_name: str, val: str, env_file_path: str = ENV_FILE_PATH) -> None:
136+
"""Persist an environment variable to a .env file.
137+
138+
Args:
139+
var_name (str): The name of the environment variable.
140+
val (str): The value of the environment variable.
141+
env_file_path (str, optional): The path to the .env file (default: ".env").
142+
"""
143+
if not os.path.exists(env_file_path):
144+
with open(env_file_path, "w") as f:
145+
f.write(f"{var_name}={val}")
146+
else:
147+
# find and replace
148+
with open(env_file_path, "r") as f:
149+
lines = f.readlines()
150+
for i, line in enumerate(lines):
151+
if line.startswith(f"{var_name}="):
152+
lines[i] = f"{var_name}={val}\n"
153+
break
154+
else:
155+
# we didn't find the variable in the file, so append it
156+
if lines[-1][-1] != "\n":
157+
lines.append("\n")
158+
lines.append(f"{var_name}={val}")
159+
with open(env_file_path, "w") as f:
160+
f.writelines(lines)
161+
162+
131163
load_dotenv()
132164

133165
for key in _DEFAULTS:

0 commit comments

Comments
 (0)